On Aug 6, 5:05 am, kangax wrote:
So to summarize (please correct me if I miss something), you
say that closure is formed when a function is being instantiated
within another function and is then made available outside of its
enclosing function.
Whether there are any free variables declared in the scope of
enclosing function is considered irrelevant. How inner function
is made available outside of its enclosing function is also
considered irrelevant (it could be a return value (or part of
it); it could be an assignment to a non-local variable; it could
be passed as one of the arguments to another function; or by
other means). Whether inner function is being actually executed
after enclosing function returns is considered irrelevant as well.
<snip>
I am not sure that that last sentence should be included. This is one
place where John G Harris' "scoping rule !== closure" comes in.
Consider a programming language that is exactly like ECMAScript in
every respect except that the named properties of activation/variable
objects that are created during variable instantiation are deleted as
execution contexts are returned from. This is then a language in which
closures don't exist, yet the behaviour of an inner function called
(even very indirectly) before the function that contains it (within
which it was created) returns is identical to what it would be in
ECMAScript. That behaviour is explained by the language's scoping
rules alone and doesn't require closures.
For a closure I think you want to see both a function object _and_ its
environment outliving their normal (minimum, given the uncertain
nature of garbage collection) lifespan.
This is, of course, an arguable position, even a purely philosophical
position. If all the structures and mechanisms that facilitate
closures are in place why shouldn't a closure be considered to exists?
The same goes for the case where you preserve the inner function but
do not employ its environment (no other inner functions, formal
parameters or variables (or no uses of any of them in the inner
function)); if there is no way to determine whether the environment
was preserved (so no consequences either way) is it worth labelling it
a closure just because the necessary mechanism and structures may be
in place?
Earlier someone mentioned the question of whether if tree falls in a
forest, does it still makes a sound if there is nobody around to
perceive it. A pragmatic attitude might suggest that if you have no
way of knowing if/when a tree falls there is little point in expending
any effort worrying about how much noise it may make if it did.
Richard.