将传递对您正在使用 .bind 的对象的引用,从而导致循环引用

发布于 2024-12-01 00:07:33 字数 987 浏览 4 评论 0原文

我有一个 jQuery 对象,我使用 .bind() 方法将事件分配给该对象。不过,我还将对对象本身的引用传递给绑定方法,如下所示:

$( document ).ready(function ()
{
    // Grab the jQuery version of the DOM element.
    var $formField1 = $( "#form-field-1" );
    // I should probably store this stuff in $formField1.data(),
    //  but not until I find out if this can cause a circular reference.
    var formFields = {
        "jQ": $formField1,
        "$comment": $( "#form-field-1-comment" ),
        "commentAnswers": [ 2, 4 ]
    };
    // Set up the comment to show when a certain answer is given.
    this.jQ.bind( "change", formFields, toggleComment );
});

function toggleComment( p_event )
{
    // Show/hide comments based on the answer in the commentAnswers array.
    if ( $.inArray($(this).val(), question.commentAnswers) > -1 )
    {
        question.$comment.parent().slideDown();
    }
    else
    {
        question.$comment.parent().slideUp();
    }
}

我想知道这是否“实际上”会导致循环引用?

I have a jQuery object, and I'm using .bind() method to assign an event to that object. However I'm also passing a reference to the object itself to the bind method as well like so:

$( document ).ready(function ()
{
    // Grab the jQuery version of the DOM element.
    var $formField1 = $( "#form-field-1" );
    // I should probably store this stuff in $formField1.data(),
    //  but not until I find out if this can cause a circular reference.
    var formFields = {
        "jQ": $formField1,
        "$comment": $( "#form-field-1-comment" ),
        "commentAnswers": [ 2, 4 ]
    };
    // Set up the comment to show when a certain answer is given.
    this.jQ.bind( "change", formFields, toggleComment );
});

function toggleComment( p_event )
{
    // Show/hide comments based on the answer in the commentAnswers array.
    if ( $.inArray($(this).val(), question.commentAnswers) > -1 )
    {
        question.$comment.parent().slideDown();
    }
    else
    {
        question.$comment.parent().slideUp();
    }
}

I want to know if this will "in fact" cause a circular reference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2024-12-08 00:07:33

这不是循环引用,但它是多余的。触发事件的对象将通过事件处理程序内的 this 可用。没有必要传递它。

但是,重要的是要认识到,设置时传递到 bind 的数据是静态的。然而,事件处理程序内的 this 将始终存储触发事件的特定对象。这两个对象可能相同,也可能不同,具体取决于绑定的应用范围。

It's not a circular reference, but it is redundant. The object triggering the event will be available through this inside the event handler. It's not necessary to pass it in.

However, it's important to realize that the data passed into bind when it's set is static. Whereas, this inside the event handler will always store the particular object that triggered the event. Those two objects may be the same or they may be different, depending on how widely the bind is applied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文