Search This Blog

Saturday, August 15, 2009

JavaScript, Closure

"In JavaScript, if you use the function keyword inside another function, you are creating a closure.

In C, and most other common languages after a function returns, all the local variables are no longer accessible because the stack-frame is destroyed.

In JavaScript, if you declare a function within another function, then the local variables can remain accessible after returning from the function you called."

Also check out the example of the infamous JavaScript loop, to understand how to get it correct using closure and assigning the result of invoking function.
And the private trick by using

Yahoo JavaScript Module Pattern

http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/

Reference:
http://blog.morrisjohns.com/javascript_closures_for_dummies

No comments: