function makeCounter(){ var obj = {counter: 0}; return { inc: function(){obj.counter ++;}, get: function(){return obj.counter;} }; } counter1 = makeCounter(); counter2 = makeCounter(); counter1.inc(); alert(counter1.get()); // returns 1alert(counter2.get()); // returns 0
最近在看闭包,我能理解的程度是外面的函数返回两个函数,应该是个闭包,其余就不知道这段代码干了什么事情了
慕慕森
相关分类