Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Last active March 12, 2016 06:18
Show Gist options
  • Save black-black-cat/2097cabb19795a2b615f to your computer and use it in GitHub Desktop.
Save black-black-cat/2097cabb19795a2b615f to your computer and use it in GitHub Desktop.
不使用return的闭包
var outer=null;
var fn=function(){
var a=1;
// 闭包可以访问函数内部的变量
outer=function(){
a++;
console.log(a);
}
}
// 调用fn后,outer有可能用到fn中定义的a,所以a不会被清除,a的生命周期得以延长
fn();
outer(); // log -> 2
outer(); // log -> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment