Answer the question
In order to leave comments, you need to log in
Why does a javascript closure behave this way?
In one of the js screencasts, I saw an example, the performance of which does not fit in my head:
While I was formulating the question, I already reached the answer myself, but still I want to understand whether I came to the right result. :)
function foo(){
var a = [];
for(var i=0; i<3; i++) {
a.push(function(){console.log(i)})
}
return a;
}
var x = foo();
x[0]();
x[1]();
x[2]();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question