According to Wikipedia's definition, as mentioned in the question, closure is
- - , ( ) .
, , , , , , /?
.
8.6.2 ECMA 262 v 5 ECMAScript. 9, [[Scope]]. ,
, , Function. ECMAScript Function [[Scope]].
, [[Scope]] . 13.2, . ( : ECMAScript, ).
, [[Scope]] VariableEnvironment, LexicalEnvironment , , , .
, , . , , , 13.2. :
// The global execution context has already been initialized at this stage.
// Declaration binding instantiation has occurred and the function
// foo is created as a new native object with a [[Scope]] property
// having the value of the global execution context VariableEnvironment
function foo() {
// When foo is invoked, a new execution context will be created for
// this function scope. When declaration binding instantiation occurs,
// bar will be created as a new native object with a [[Scope]] property
// having the value of the foo execution context VariableEnvironment
function bar() {
}
bar(); // Call bar
}
foo();
, - , / . , .
- , NewDeclarativeEnvironment. [[Scope]] , " ". (, [[Scope]] . , Lexical Environment - , , , .)
- LexicalEnvironment VariableEnvironment 1.
- .
, [[Scope]], , .
<script>
function foo() {
var x = 1;
function bar() {
var y = 2;
alert(x + y);
}
return bar;
}
var dummy = foo();
dummy();
alert(dummy.name);
</script>
, , parent LexicalEnvironment [[Scope]] . , , " " [[Scope]].
: , , .