jquery - 将传递对您正在使用 .bind 的对象的引用会导致循环引用

标签 jquery circular-reference

我有一个 jQuery 对象,我正在使用 .bind() 方法将事件分配给该对象。不过,我还将对象本身的引用传递给绑定(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();
    }
}

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

最佳答案

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

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

关于jquery - 将传递对您正在使用 .bind 的对象的引用会导致循环引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7152197/

相关文章:

c# - 使用什么序列化程序(到 xml)进行循环引用和自定义字段?

c++ - 关于 "circular reference",我使用了 weak_ptr 但内存泄漏仍然发生

javascript - Highcharts 、仪表图表、 slider 更新

jquery - 在鼠标悬停时添加边框会移动内容

c# - AutoMapper:PreserveReferences 和 MaxDepth 有什么区别?

sql - 使用循环引用 SQL 在表中插入

ocaml - 在同一记录的其他字段中使用记录字段

jquery - 处理多个 ajax 请求 - 仅使用最后一个请求

javascript - jquery-ui : detect first time change in input

javascript - jQuery 移动按钮有时只会被选中,但不会发生页面转换