1 //闭包 ,嵌套函数被导出到它所定义的作用域外时,以这种方法调用,叫做一个闭包 2 function outA() { 3 var x = 0; 4 function inA() { 5 x += 1; 6 console.log("inA:x=" + x); 7 } 8 return inA; 9 }10 var funa = outA();11 funa();12 funa();13 14 15 16 function outC(obj) {17 var x = 0;18 function inA() {19 x += 10;20 console.log("inA:x=" + x);21 }22 function inB() {23 x += 2;24 console.log("inB:x=" + x);25 }26 obj.funa = inA;27 obj.funb = inB;28 }29 var o = new Object();30 outC(o);31 o.funa();32 o.funb();
输出:"inA:x=1" "inA:x=2" "inA:x=10" "inB:x=12"
注:还差函数的使用与调用,就与Arguments合并新闻热点
疑难解答