this doesn't :
(function(){
function f () {};
})();
but this does:
(function(){
function f () {};
window.onload= f;
})();
That probably depends on exactly how you define a closure.
Any optimizations aside, when function is instantiated, it must
initialize (or at least behave as if it initializes) its internal
[[Scope]] property to contain the same objects as those in a scope chain
of current execution context (the one it's being instantiated within).
That [[Scope]] is then used to initialize function's execution context
(where it will be used entirely and only preceded with function's
Activation Object).
This means that in your first example -
(function(){
function f(){};
})();
- when `f` is being instantiated, its [[Scope]] contains at least outer
function's Activation Object. Whether there are any variable/function
declarations performed in the outer function (so that Activation Object
has other properties but `arguments`) is technically irrelevant; it's
already closed over inner function and so will be used when initializing
inner function's execution context.
If we consider this process to be a closure, than we can say that even
in a simple (and pointless) case like this -
function f(){
function g(){}
}
- a closure is being formed with inner function (over outer function).
On the other hand, we can make definition a bit stricter and say that
closure is formed only when there exist any free variables in any of the
activation objects (not counting implicit `arguments` property) captured
in function's [[Scope]], such as -
function f(){
var x;
function g(){}
}
We can also say that closure is formed only when inner function is being
made available to the outside of its containing function (which, I
think, is what you were trying to say), such as -
function f(){
function g(){}
return g;
}
Or we can have both prerequisites -
function f(){
// free variable
var x;
// and function is made available for outside execution
setTimeout(function(){
return x;
});
}
which would mean that both -
(function(){
return function(){};
})();
// and
(function(){
var x;
(function(){})();
})();
- would be considered as those NOT forming the closure.
I was never really sure which one of these people mean when talking
about closures. For example, Richard's article - the most definite
description of closures to date - has a definition (coined by Lasse, I
think) which seems to "fit" first, least strict, case -
| A "closure" is an expression (typically a function) that can have free
| variables together with an environment that binds those variables
| (that "closes" the expression).
But then there's a more explicit explanation, and it already mentions
"outside availability" of inner function -
| A closure is formed when one of those inner functions is made
| accessible outside of the function in which it was contained, so that
| it may be executed after the outer function has returned.
I'm actually curious which one of these examples can be considered to be
"forming a closure". I, for example, always thought (and it looks like
RobG thought so as well) that first example already does.
Perhaps, I'm just missing something here.