Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>(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
       ...
      })()


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.


It's a WTF in the sense that every other language with curly braces scope at the block level.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: