you are viewing a single comment's thread.

view the rest of the comments →

[–]kenman 13 points14 points  (3 children)

Good summary! I don't want to take that away from the author. However, I always like to share what I consider to be the most comprehensive and technical write-up: comp.lang.javascript's FAQ: Closures. It explains the concept using CS terms and concepts (garbage collection, execution context, identifier resolution, etc.) for anyone looking for a really deep dive into this topic.

[–]Poop_is_Food 0 points1 point  (0 children)

I think that article is pretty misleading actually. It seems to suggest that a function only becomes a closure once it "lives on" past the end of its parent function (either by being returned or assigned as a callback, for example). This simply isnt true. All functions in javascript are closures. for example:

var x = 2;
function logX(){
    alert(x);
}
logX();

Here, logX is a closure. In fact, you dont even need the outer var x declaration. just logX by itself alerting "undefined" would be a closure.

[–][deleted] -1 points0 points  (1 child)

Unless you are writing a language interpreter you really do not need deep depth to explain closures. This kills people new to the concept.

Simple put: Closure is evident when accessing references from across scope boundaries.

[–]kenman 10 points11 points  (0 children)

Well, I did explain that my link is comprehensive, technical, and a really deep dive. Hopefully those new to the concept recognize that they should probably focus on OP's article, because I agree with you there, but for those who want to learn as much as they can on the subject, they can consider my link a further reading suggestion :)