http://stackoverflow.com/questions/111102/how-do-javascript-closures-work

Also a decent link from one of those answers, though the explanation is bit long.

http://jibbering.com/faq/notes/closures/

I think the one I like best, though, comes from here, even if the guy's a little snotty replying to one of the comments to his post:

I find the best way to explain them (and the way that I learned what they do) is to imagine the situation without them:

var bind = function(x) {
return function(y) { return x + y; };
}

var plus5 = bind(5);
alert(plus5(3));

What would happen here if JavaScript didn't know closures? Just replace the call in the last line by its method body (which is basically what function calls do) and you get:

alert(x + 3);

Now, where's the definition of x? We didn't define it in the current scope. The only solution is to let plus5 carry its scope (or rather, its parent's scope) around. This way, x is well-defined and it is bound to the value 5.

Labels: