Closure闭包示例
var foo = function(){ var cnt = 0; return function(){ return cnt++; }; }; var closure = foo(); console.log(closure()); console.log(closure()); console.log(closure());
一个可调用对象引用一外部的变量(但又不是全局变量)时,就形成了闭包。
var foo = function(){ var cnt = 0; return function(){ return cnt++; }; }; var closure = foo(); console.log(closure()); console.log(closure()); console.log(closure());
一个可调用对象引用一外部的变量(但又不是全局变量)时,就形成了闭包。