Closures in JavaScript

I have been trying to grapple or get my head around “closures” and their necessity in problem-solving. Here are some of my thoughts.
a) Closure – is a “stack/frame” of memory that is accessible when a function returns with the “state” of the function. Added to the definition, is a function only “returns” a closure, when a function contains a nested function.
That is:
Note – the first alert will return nothing but a string version of the method (that is itemDownloading) – however it will return a ‘stack’ with state.
You can then call “itemDownloading” which will now call the “update” (inner function) and return 30.
Wikipedia also has a pretty sweet definition:
“A closure is a function defined within another scope that has access to all the variables within the outer scope.”
Other examples as it takes a while before it sticks:

Don't be shellfish...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on TumblrEmail this to someone

Leave a Reply