Closures Explained

D

David Mark

function attachEvents() {
  var el, index = document.getElementsByTagName('div').length;
  while (index--) {
     el = document.getElementsByTagName('div')[index];
Are you suggesting that calling getElementsByTagName() in a loop is more
efficient than storing a reference to the collection in a variable, and
Yes.  Browsers optimize for that pattern.

I would also like to see a source for that.
Doubt it all you want.
Nonsense.  Opera has a long article on their site about this.

Well. Here I am, having tested this in 5 current browsers (not

Yes, there you are. Your testing has been previously proven suspect.
Pardon me if I am non-responsive.
 
R

RobG

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 >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,138
Messages
2,570,801
Members
47,348
Latest member
nethues

Latest Threads

Top