On Tue, 26 Apr 2011 17:48:45 +0200, Nick Morgan <skilldr...@gmail.com>
wrote:
Hi all
I've written a new post on my blog, "Closures explained with JavaScript".
http://skilldrick.co.uk/2011/04/closures-explained-with-javascript/
Mentioning it here for two reasons
(a) to help the mentees understand this crucial (and often confusing)
aspect of JS, and
(b) to ask the mentors if they have any
feedback/criticism/clarifications...
Minor nitpicks only. I like the description.
"The ability to nest functions gives us closures."
I'd say that even top-level function expression evaluate to closures. The
scope they
capture might just be the global scope, but it's still there.
In a language with first-class functions and static scope, all functions
are (possibly
trivial) closures, where the trivial case is a function with no free
variables (which
can arguably be said to not be a closure).
As you have said, a closure is code combined with an environment where the
free variables
are looked up. A top-level function is that, with the global scope as its
environment.
"A variable is free in any particular scope if it is defined within an
enclosing scope."
I would normally not separate a nested scope from the surrounding scope -
it's more
like an extension of the outer scope. I would prefer to talk about
variables being free
in an expression/statement/function declaration, i.e., at the syntactic
level. However,
it still makes sense as written, so that's just me preferring my
preference :)
Apart from that, in most other languages, I would agree with the
statement, because it's
equivalent to being a variable that's not bound by a declaration in the
function declaration
we are focusing on. Not so in Javascript. In Javascript you can create
functions with free
variables that aren't defined anywhere (yet), because the scope chain is
mutable. You can
add global, and even local, variable declarations after you have created
the function object.
So, I'd say something like:
A variable is free in a function's code, if it's not bound by a variable
declaration
inside that function. Resolving that variable will use the scope where
the function
occurs.
or something to that effect, which I think you can make more readable than
I can.
(Although that still ignores the case where the variable may or may not be
declared
dynamically inside the function).
It's much harder to describe this in Javascript than in, say, Scheme,
because variables
in Javascript are resolved every time they are accessed, and the scope
chain may change
between each access.
"Closures are functions that retain a reference to their free variables"
Again, in other languages this would be the whole truth. In Javascript,
the closure
retains a reference to the environment where variables will be looked up.
That might just be a little too tricky for this blog post, though. No need
to confuse
people more than necessary :)
I really like the summary. It sums up the general concept very well.
/L ' http://fiddle.jshell.net/Gmwuw/ '
--
Lasse Reichstein Holst Nielsen - reichsteinatw...@gmail.com
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/jsmentors@jsmentors.com/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/jsmentors@googlegroups.com/
To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com