JavaScript闭包读书笔记
var scope = "global scope";
function checkscope() {
var scope = "local scope";
function f() { return scope;}
return f();
}
checkscope()
var scope = "global scope";
function checkscope() {
var scope = "local scope";
function f() {return scope;}
return f;
}
checkscope()
checkscope()()
var scope = "global scope";
function checkscope(){
var scope = "local scope";
var self = this;
function f(){
return self.scope;
}
return f();
}
checkscope()
var uniqueInteger = (function(){
var counter = 0;
return function(){
return counter++;
}
}())
var uniqueInteger = function(){
var counter = 0;
return {
count: function(){return counter++;},
reset: function(){return counter=0;}
}
}
a = uniqueInteger()
b = uniqueInteger()
a.count()
a.count()
b.count()
a.reset()
a.count()
function constfunc(){
var funcs = [];
for (var i=0; i<10; i++){
funcs[i] = function(){return i;}
}
return funcs;
}
var funcs = constfunc()
funcs[5]()
funcs[5]()