22 points · by wilsonfiifi · 12y ago · 16 comments
Meaning this piece of code works! Blimey!
for(var i=0; i<=10; i++){
// do nothing
}
// access 'i' out of for loop
console.log("i = " + i); // i = 11Browser compatibility isn't too bad, but no Safari support it seems: http://kangax.github.io/es5-compat-table/es6/#let
var foo = function(callback) { callback(); }
for(var i=0; i<=10; i++){
setTimeout(function() {
console.log("i = " + i);
}, 100);
}
Which, if you don't know what to look for, is a bit of a brain melter. Between that and [1,2,10].sort(), there are lots of great reasons to yell at your screen when learning JS. array.forEach(function(value, index) { ... });Aside from that: Function scope makes sense to me but I dont think I've ever gone and thought "damn it all, if only I had block scope!"
I don't think anyone really sees a problem with it, although it might be somewhat of a gotcha for people coming from languages where variables are local to the loop.
As it is, without that explanation, all I can think is, so weird language x does something weird and then its a gotcha when a language (funnily enough javascript) does it in a non weird way? I'd say sounds more like a gotcha for language x, than anything else.