This might have been asked several times, but I don't find it. The following code does - of course - not what it obviously intends:
var arr = [];
var count = 10;
var funct = function() { console.log("count when called: ", count); };
while (count--) {
arr.push(funct);
}
for (i in arr) {
arr[i]();
}
I got logged -1 in each for loop, because the function accesses the count value of the environment in which it was created. and count has the -1 value after the while loop.
What I need is something like passing an argument to the creation of the function. Any hints?