I've rewritten a short article explaining closures in JavaScript.
It's
at:
http://www.martinrinehart.com/articles/javascript-closures.html
A big Thank You to PointedEars and Jorge for helping me get closer to
the truth.
Seems the article hasn't been updated since you started this thread,
are you going to?
Don't be discouraged by criticism, teaching something is a very good
way to learn it thoroughly if you are prepared to learn yourself from
the experience.
A couple of tips, some mentioned by others, some not:
1. Closures are a feature of ECMAScript. Javascript is not a subset
of ECMAScript, quite the opposite, so I'd remove the note below the
title. Crockford’s book is just his opinion on some features of the
language that he considers good/bad/ugly, I don’t think it’s a serious
attempt to modify the standard (although I haven’t read it).
2. The article’s definition of a closure is meaningless to me, but
then again, I haven’t read a one-line description that makes sense. To
me a closure is the ability to keep a reference to a function variable
after the function has finished executing. That isn’t particularly
accurate from a technical viewpoint, but it does the job for me.
3. In the “Why do you want closures” section, the statement “...the
closure is invisible except to the singe public function of the
closure” is misleading: any number of functions can have closures to
the variables of a function. Also, if the functions that have those
references are public, they can be called privileged (see Crockford’s
article on public and private members in javascript[1]). A reference
to that article should be included.
4. A closure is a consequence of declaring one function inside another
using either a formal declaration, function expression or statement.
It’s only useful if some reference is kept of course, but the closure
isn’t the variable reference, it’s the ability of the “inner” function
to access the activation object of the outer function and its
variables.
I think you should reference the FAQ article[2] which is not only a
comprehensive and technically excellent article on closures, it
teaches an awful lot about the fundamentals of functions in general.
1. Douglas Crockford, Private Members in JavaScript :
<URL:
http://javascript.crockford.com/private.html>
2. Richard Cornford, et al, Javascript Closures
<URL:
http://www.jibbering.com/faq/faq_notes/closures.html >