If you define a function n() nested inside f(), n() will have access to variables in its own scope, plus the scope of its "parents". This is known as scope chain, and the chain can be as long (deep) as you need it to be
var a = 1;
function f() {
var b = 1;
function n() {
var c = 3;
}
}
Lexical Scope
In JavaScript, functions have lexical scope
This means that functions create their environment (scope) when they are defined, not when they are executed
>>> function f1(){var a = 1; f2();}
>>> function f2(){return a;}
>>> f1();
a is not defined
At the time when f2() was defined (as opposed to executed), there was no a in sight.
f2(), just like f1(), only has access to its own scope and the global scope. f1() and f2() don't share their local scopes.
When a function is defined, it "remembers" its environment, its scope chain
>>> var a = 5;
>>> f1();
5
>>> delete a;
true
>>> f1();
a is not defined
>>> delete f2;
true
>>> f1()
f2 is not defined
Breaking the Chain with a Closure
If you're at point a, you're inside the global space
If you're at point b, which is inside the space of the function F, then you have access to the global space and to the F-space
If you're at point c, which is inside the function N, then you can access the global space, the F-space and the N-space
the closure—happens when somehow N breaks out of F and ends up in the global space
This is interesting, because N is where a is and yet N does have access to b, but a doesn't
And how does N break the chain?
- By making itself global (omitting var)
- by having F deliver (or return) it to the global space
Closure #1
function f() {
var b = "b";
return function() {
return b;
}
}
>>> b
b is not defined
>>> var n = f();
>>> n();
"b"
f is a global function. Calling f returned function with return value b to the global space. This function is assigned to variable n. So calling n now returned b.
Closure #2
var n;
function f() {
var b = "b";
n = function() {
return b;
}
}
>>> f();
>>> n();
"b"
Closure #3
function f(arg) {
var n = function() {
return arg;
};
arg++;
return n;
}
>>> var m = f(123);
>>> m();
124
Closures in a Loop
function f() {
var a = [];
var i;
for (i = 0; i < 3; i++) {
a[i] = function() {
return i;
}
}
return a;
}
>>> var a = f();
>>> a[0]()
3
>>> a[1]()
3
>>> a[2]()
3
Closures don't remember the value, they only link (reference) the i variable and will return its current value
After the loop, i's value is 3. So all the three functions point to the same value.
function f() {
var a = [];
var i;
for (i = 0; i < 3; i++) {
a[i] = (function(x) {
return function() {
return x;
}
})(i);
}
return a;
}
Alternatively,
function f() {
function makeClosure(x) {
return function() {
return x;
}
}
var a = [];
var i;
for (i = 0; i < 3; i++) {
a[i] = makeClosure(i);
}
return a;
}
Getter/Setter
var getValue, setValue;
(function() {
var secret = 0;
getValue = function() {
return secret;
};
setValue = function(v) {
secret = v;
};
})()
>>> getValue()
0
>>> setValue(123)
>>> getValue()
123
Iterator
function setup(x) {
var i = 0;
return function() {
return x[i++];
};
}
>>> var next = setup(['a', 'b', 'c']);
>>> next();
"a"
>>> next();
"b"
>>> next();
"c"




No comments:
Post a Comment