Hacker News new | past | comments | ask | show | jobs | submit login

The "WAT" for me is that the `var i` is hoisted _out_ of the `for`, which IMHO, breaks the principle of least astonishment. I would expect the `i` to be lexically scoped to the body of the `for` block, instead of being hoisted out into the parent scope of the `for` block.



I speculate that this is because Javascript takes stuff from Self where loops are functions.

So probably Javascript was meant to write something like: while(function(k) { ++i; return i < k; });

where the while function would call the closure until it got false at which point the while function itself would return so having var declarations be hoisted was not a problem in this syntax because everything (including if and while) would have its own scope due to being a function


You could have designed a language like that, but Brendan didn't.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: