On Tue, 26 Apr 2011 17:48:45 +0200, Nick Morgan <skilldr...@gmail.com>
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
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
capture might just be the global scope, but it's still there.
In a language with first-class functions and static scope, all functions
trivial) closures, where the trivial case is a function with no free
can arguably be said to not be a closure).
As you have said, a closure is code combined with an environment where the
are looked up. A top-level function is that, with the global scope as its
"A variable is free in any particular scope if it is defined within an
I would normally not separate a nested scope from the surrounding scope -
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
it still makes sense as written, so that's just me preferring my
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
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
inside that function. Resolving that variable will use the scope where
or something to that effect, which I think you can make more readable than
(Although that still ignores the case where the variable may or may not be
dynamically inside the function).
chain may change
between each access.
"Closures are functions that retain a reference to their free variables"
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
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:
To search via a non-Google archive, visit here:
To unsubscribe from this group, send email to