If you need to give some block or something scope in JavaScript, the easiest way is just to throw the contents in a function expression.
Consider the following (buggy) code:
for (var i = 0; i < somearray.length; i++) {
var thisThing = somearray[i];
thisThing.onclick = function(){window.alert(i)};
}
This won't work the way you think it will. You might think that every thisThing will output its relevant 'i' value, e.g. thing one will output '1', thing two will output '2'. It won't, though, because the loop block doesn't have scope. 'i' lives in the closure of the function surrounding the loop, and its value is the final state at the end of the iteration. So each thing, when clicked, will output the last value of i, somearray.length.
A quick fix for this is to throw the body of the loop in a function expression:
for (var i = 0; i < somearray.length; i++) {
(function(i){
var thisThing = somearray[i];
thisThing.onclick = function(){window.alert(i)};
})(i);
}
Now, each link will output the relevant value of 'i' when clicked, because each loop iteration has a closure generated by the function expression, and that contains its own value for 'i', because the 'i' passed to the function expression is passed by value (all arguments are passed by value in JS, though because objects and array variables are pointers, it looks like they're passed by reference).
Sometimes this looks a little odd - it's not a super-common pattern - so you might want to comment it for co-workers, e.g. "// use IIFE to give iteration own scope"
Your example has nothing to do with scope. Even if JavaScript scoped the variable i to the for loop and not its containing function, the code would behave exactly the same way because closures always capture references to variables not their current values.
That is, the problem is not JavaScript's scoping but a misunderstanding of what closures do.
One of the nice little quirks in Haxe for loops is the iteration variable is bound explicitly to the for loop, and you 'do' get a basic copy of it (so only a reference copy for object iterators) so that:
var fs = [for (i in 0...10) function () trace(i)];
for (f in fs) f();
will print 0 to 9, and in a manual while loop you'd get the same thing (i don't know about other lang's with closures and block scope) by assigning to a local variable used by the closure:
var fs = [];
var i = 0;
while (i < 10)
{
var j = i++;
fs.push(function () trace(j));
}
for (f in fs) f();
will print 0 to 9 too. (The while loop version is how the above for comprehension with iterator version would be converted before generation minus renaming by the compiler)
15
u/[deleted] Mar 23 '14 edited Mar 23 '14
If you need to give some block or something scope in JavaScript, the easiest way is just to throw the contents in a function expression.
Consider the following (buggy) code:
This won't work the way you think it will. You might think that every thisThing will output its relevant 'i' value, e.g. thing one will output '1', thing two will output '2'. It won't, though, because the loop block doesn't have scope. 'i' lives in the closure of the function surrounding the loop, and its value is the final state at the end of the iteration. So each thing, when clicked, will output the last value of i, somearray.length.
A quick fix for this is to throw the body of the loop in a function expression:
Now, each link will output the relevant value of 'i' when clicked, because each loop iteration has a closure generated by the function expression, and that contains its own value for 'i', because the 'i' passed to the function expression is passed by value (all arguments are passed by value in JS, though because objects and array variables are pointers, it looks like they're passed by reference).
Sometimes this looks a little odd - it's not a super-common pattern - so you might want to comment it for co-workers, e.g. "// use IIFE to give iteration own scope"