Jquery 是否将引用传递给您';重复使用.bind导致循环引用
我有一个jQuery对象,我正在使用Jquery 是否将引用传递给您';重复使用.bind导致循环引用,jquery,circular-reference,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 $formField
.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
的数据是静态的。然而,事件处理程序中的此
将始终存储触发事件的特定对象。这两个对象可能相同,也可能不同,这取决于bind
的应用范围。什么是this.jQ
?你是说formFields.jQ
?在这种情况下,只需使用$formField1
。在原始代码中有一个$。每个循环对表单中的所有表单字段执行。我想排除不相关的代码,只是错过了将其更改为$formField1。今天早上我发现它是多余的,如果我只是使用.data()方法将commentAnswers和$comment附加到对象本身,那么整个问题就可以避免了。答案仍然很重要。谢谢!想到这可能是一个循环引用,我几乎有一次焦虑发作。