Rectangle 27 0

JavaScript closure inside loops – simple practical example?


var funcs = [];
for(var i =0; i<3; i++){
    funcs[i] = function(){
        alert(i);
    }
}

for(var j =0; j<3; j++){
    funcs[j]();
}
var funcs = [];
for(var new_i =0; new_i<3; new_i++){
    (function(i){
        funcs[i] = function(){
            alert(i);
        }
    })(new_i);
}

for(var j =0; j<3; j++){
    funcs[j]();
}

Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

Self-calling, or self-invoking is not the appropriate term for this technique, IIFE (Immediately-Invoked Function Expression) is more accurately. Ref: benalman.com/news/2010/11/

The idea behind this is, encapsulating the entire body of the for loop with a IIFE (Immediately-Invoked Function Expression) and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering from this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

which which alters "2", for 3 times. This is because anonymous functions created in for loop, shares same closure, and in that closure, the value of i is the same. Use this to prevent shared closure,

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


var funcs = [];
for(var i =0; i<3; i++){
    funcs[i] = function(){
        alert(i);
    }
}

for(var j =0; j<3; j++){
    funcs[j]();
}
var funcs = [];
for(var new_i =0; new_i<3; new_i++){
    (function(i){
        funcs[i] = function(){
            alert(i);
        }
    })(new_i);
}

for(var j =0; j<3; j++){
    funcs[j]();
}

Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


var funcs = [];
for(var i =0; i<3; i++){
    funcs[i] = function(){
        alert(i);
    }
}

for(var j =0; j<3; j++){
    funcs[j]();
}
var funcs = [];
for(var new_i =0; new_i<3; new_i++){
    (function(i){
        funcs[i] = function(){
            alert(i);
        }
    })(new_i);
}

for(var j =0; j<3; j++){
    funcs[j]();
}

Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


var funcs = [];
for(var i =0; i<3; i++){
    funcs[i] = function(){
        alert(i);
    }
}

for(var j =0; j<3; j++){
    funcs[j]();
}
var funcs = [];
for(var new_i =0; new_i<3; new_i++){
    (function(i){
        funcs[i] = function(){
            alert(i);
        }
    })(new_i);
}

for(var j =0; j<3; j++){
    funcs[j]();
}

Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


var funcs = [];
for(var i =0; i<3; i++){
    funcs[i] = function(){
        alert(i);
    }
}

for(var j =0; j<3; j++){
    funcs[j]();
}
var funcs = [];
for(var new_i =0; new_i<3; new_i++){
    (function(i){
        funcs[i] = function(){
            alert(i);
        }
    })(new_i);
}

for(var j =0; j<3; j++){
    funcs[j]();
}

Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


var funcs = [];
for(var i =0; i<3; i++){
    funcs[i] = function(){
        alert(i);
    }
}

for(var j =0; j<3; j++){
    funcs[j]();
}
var funcs = [];
for(var new_i =0; new_i<3; new_i++){
    (function(i){
        funcs[i] = function(){
            alert(i);
        }
    })(new_i);
}

for(var j =0; j<3; j++){
    funcs[j]();
}

Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note
Rectangle 27 0

JavaScript closure inside loops – simple practical example?


var funcs = [];
for(var i =0; i<3; i++){
    funcs[i] = function(){
        alert(i);
    }
}

for(var j =0; j<3; j++){
    funcs[j]();
}
var funcs = [];
for(var new_i =0; new_i<3; new_i++){
    (function(i){
        funcs[i] = function(){
            alert(i);
        }
    })(new_i);
}

for(var j =0; j<3; j++){
    funcs[j]();
}

Read something similar in a book once. I prefer this, too, since you don't have to touch your existing code (as much) and it becomes obvious why you did it, once you've learned the self-calling function pattern: to trap that variable in the newly created scope.

The idea behind this is, encapsulating the entire body of the for loop with a self calling anonymous function and passing "new_i" as a parameter and capturing it as "i". Since the anonymous function is executed immediately, the "i" value is different for each function defined inside anonymous function. This solution seems to fit any such problem, since it will require minimum changes to original code suffering for this issue. In fact this is by design, it should not be an issue at all!

The most simple solution would be

instead of using this

Note