This is not a WTF if you just know where a language draws scope boundaries - for most languages, it isn't just along the {}'s.
Re:WTFJS?
by statictype on Sunday February 14, 2010 @02:26AM
(#1124276)
It's a WTF in the sense that every other language with curly braces scope at the block level.
>JavaScript interpreter checks all variable declarations in scope before execution.
This is another way of saying that all variable declarations are scoped at the function level, not the individual block level. This is why generating closures in a loop is broken in javascript (Although in just about every other language as well, closures in a loop give surprising results at first - including, I think, arc?)
To get a new scope, you have to use the anonymous function trick: