News for hackers. Stuff that matters.
Re:JavaScript Interview Question by broquaint on Thursday January 20, 2011 @12:55PM (#2124521)
The behaviour is due to the fact that variables in JavaScript are scoped to the function, although as that code is in the global scope and lacks a var declaration it will live under the global object (window in this case). So in that example each closure captures the same variable hence 4 being produced within each alert(). For more info see: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/C...

There are no comments on this comment yet.