Sure.
In your for
loop, you reference i
. What you expect to be happening is that each closure gets a snapshot of i at the time the function is created, therefore in the first function it would return 0, then 1, etc.
What's really happening is that each closure is getting a reference to the external variable i
, which keeps updating as you update i
in the for
loop.
So, the first time through the loop, you get a function that returns i
, which at this point is 0. The next time you get two functions which return i
, which is now 1, etc.
At the end of the loop, i==10
, and each function returns i
, so they all return 10.
UPDATE TO ADDRESS QUESTION IN COMMENT:
It's a little confusing since you use i
in two different contexts. I'll make a very slight change to your code to help illustrate what's going on:
function creatFunctions() {
var result = new Array();
for (var i = 0; i < 10; i++) {
result[i] = function () {
return i;
}
}
return result;
}
var funcs = creatFunctions();
// NOTE: I changed this `i` to `unrelated_variable`
for (var unrelated_variable = 0; unrelated_variable < funcs.length; unrelated_variable++) {
document.write(funcs[unrelated_variable]() + "<br />");
}
... the functions that you create in your creatFunctions()
function all return i
. Specifically, they return the i
that you create in the for loop.
The other variable, which I've renamed to unrelated_variable
, has no impact on the value returned from your closure.
result[i] = function () {
return i;
}
... is not the same thing as result[2] = 2
. It means result[2] = the_current_value_of_i