3 ms·
>(function(){ if(false) var window; return window }()) === window >JavaScript interpreter checks all variable declarations in scope before execution. This is
by statictype 17y ago
>(function(){ if(false) var window; return window }()) === window
>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:
(function(){
var x;//new scope
...
})()
- andreyf 17y agoThis is not a WTF if you just know where a language draws scope boundaries - for most languages, it isn't just along the {}'s.
- statictype 17y agoIt's a WTF in the sense that every other language with curly braces scope at the block level.