https://developer.mozilla.org...
首先引用来自官网文档的定义:
closure is the combination of a function and the lexical environment within which that function was declared.
闭包是一个函数和其内部公开变量的环境的集合.
简单而言, 闭包 = 函数 + 环境
第一个闭包的例子
function init() {
var name = 'Mozilla'; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert(name); // use variable declared in the parent function
}
displayName();
}
init();
because inner functions have access to the variables of outer functions, displayName() can access the variable name declared in the parent function, init().
- 其实这个栗子很简单,displayName()就是init()内部的闭包函数,而为啥在displayName内部可以调用到外部定义的变量 name 呢,因为js内部函数有获取外部函数中变量的权限。
第二个栗子
var data = [
{'key':0},
{'key':1},
{'key':2}
];
function showKey() {
for(var i=0;i<data.length;i++) {
setTimeout(function(){
//console.log(i); //发现i输出了3次3
//console.log(this); // 发现 this 指向的是 Window
data[i].key = data[i].key + 10;
console.log(data[i].key)
}, 1000);
}
}
showKey();
上面这个例子可以正确输出 10 11 12 吗?
答案是:并不能,并且还会报语法错误....
- console.log(i); 发现i输出了3次3,也就是说,在setTimeout 1000毫秒之后,执行闭包函数的时候,for循环已经执行结束了,i是固定值,并没有实现我们期望的效果。
- console.log(this); 发现 this 指向的是 Window,也就是说,在函数内部实现的闭包函数已经被转变成了全局函数,存储到了内存中。
所以需要再定义一个执行函数
var data = [
{'key':0},
{'key':1},
{'key':2}
];
function showKey() {
var f1 = function(n){
data[i].key = data[i].key + 10;
console.log(data[i].key)
}
for(var i=0;i<data.length;i++) {
setTimeout(f1(i), 1000);
}
}
showKey();
// 得到预期的 10 11 12
第三个闭包的例子-柯里化(currying)
function makeAdder(x) {
return function(y) {
return function(z) {
return x + y + z;
}
};
}
console.log(makeAdder(1)(2)(3)); // 6
// function factory it creates functions which can add a specific value to their argument
var add5 = makeAdder(5);
console.log(add5(1)(2)); // 8
console.log(add5(4)(5)); // 14
- 这种返回function的形式就是柯里化,作用是 makeAdder 可以作为一个 function factory来使用。
第四个闭包的例子 - practicle closure
- 闭包的适用场景:当你想要通过一个function来操作它关联的数据时,闭包是很有用的,这种使用方法是类似面向对象的。
- 闭包同样可以模拟面向对象的私有变量的方法和变量的使用和获取。
var counter = (function() {
// private variable
var privateCounter = 0;
// private function
function changeBy(val) {
privateCounter += val;
}
return {
changeValue: function(val) {
changeBy(val);
},
value: function() {
return privateCounter;
}
};
})();
console.log(counter.value()); // logs 0 // 实现了内部属性的获取
counter.changeValue(2);// 实现了内部的changeBy()方法
counter.changeValue(10);
console.log(counter.value()); // logs 12
counter.changeValue(-5);
console.log(counter.value()); // logs 7
- counter对外暴露的方法就是 counter.changeValue, counter.value,而内部属性 privateCounter 和 内部方法 changeBy 被隔离开了,只能通过外部方法来调用。
- 同时我们也可以定义多个 counter,其内部属性也是相互隔离的。
var makeCounter = function() {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function() {
changeBy(1);
},
decrement: function() {
changeBy(-1);
},
value: function() {
return privateCounter;
}
}
};
var counter1 = makeCounter();
var counter2 = makeCounter();
alert(counter1.value()); /* Alerts 0 */
counter1.increment();
counter1.increment();
alert(counter1.value()); /* Alerts 2 */
counter1.decrement();
alert(counter1.value()); /* Alerts 1 */
alert(counter2.value()); /* Alerts 0 */
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。