2

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();
    }
}

これが「実際に」循環参照を引き起こすかどうか知りたいですか?

4

1 に答える 1

1

循環参照ではありませんが、冗長です。thisイベントをトリガーするオブジェクトは、イベント ハンドラー内で使用できます。渡す必要はありません。

ただし、設定時に渡されるデータbindは静的であることを認識することが重要です。一方、thisイベント ハンドラー内には、イベントをトリガーした特定のオブジェクトが常に格納されます。これら 2 つのオブジェクトは、 が適用される範囲に応じて、同じ場合も異なる場合もありますbind

于 2011-08-22T19:24:49.810 に答える