将传递对您正在使用 .bind 的对象的引用,从而导致循环引用
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是循环引用,但它是多余的。触发事件的对象将通过事件处理程序内的
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 thebind
is applied.