News for hackers. Stuff that matters.
Re:WTFJS? by statictype on Friday February 12, 2010 @09:06PM (#1122379)
>(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
       ...
      })()
Re:WTFJS? by andreyf on Friday February 12, 2010 @09:10PM (#1122391)
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.