While reading a different topic I came across this piece of code:
function loadme() {
var arr = ["a", "b", "c"];
var fs = [];
for (var i in arr) {
var x = arr[i];
var f = function() { console.log(x) };
f();
fs.push(f);
}
for (var j in fs) {
fs[j]();
}
}
This will output: a,b,c,c,c,c. The problem here as explained by the author is that the x var is hoisted at the start of the function and therefore does not keeps it's value when it is used in the loop. What I do not understand is why is c assigned to x on the second console.log for 3 times? Can someone explain?
In these two lines,
console.log(x)
thinks that it just has to printx
. So, all the three functions are created that way only. So, when you immediately execute the functions, the value ofx
is different in each of the iterations. But when the looping is over, the variablex
retains the last value it held and when the dynamically created functions are executed, they simply print the value ofx
which isc
.So, in order to fix this, you have to have your own copy of
x
, for each dynamically created function. Normally, we retain the current state of the variable which changes in the loop, with a function parameter, like thisNow, we are creating a function and executing it immediately, which returns another function and that returned function actually prints the value of
x
.We are passing the value of
x
as a parameter to the wrapper function and now the current value ofx
is retained.