Please explain closures, or binding the loop counter to the function scope

后端 未结 2 395
孤城傲影
孤城傲影 2021-02-04 17:59

I\'ve seen programmers assign events listeners inside loops, using the counter. I believe this is the syntax:

for(var i=0; i < someArray.length; i++){
   some         


        
2条回答
  •  星月不相逢
    2021-02-04 18:42

    This is done because JavaScript only has function scope, not block scope. Hence, every variable you declare in a loop is in the function's scope and every closure you create has access to the very same variable.

    So the only way to create a new scope is to call a function and that is what

    (function(i){/* Some code using i */}(i))
    

    is doing.

    Note that your example misses an important part: The immediate function has to return another function which will be the click handler:

    someArray[i].onclick = (function(i){
        return function() {
           /* Some code using i */
        }
    }(i));
    

    The immediate function is nothing special. It is somehow inlining function definition and function call. You can replace it by a normal function call:

    function getClickHandler(i) {
        return function() {
             /* Some code using i */
        }
    }
    
    for(var i=0; i < someArray.length; i++){
       someArray[i].onclick = getClickHandler(i);
    }
    

提交回复
热议问题