实现代码如下:

var test=(function() { var arr2=[1,2,3,3]; return function(){ return arr2; }; })()
Array.prototype.f1= function () { return []; }
Array.prototype.f2= function () { this.length=0; return this; }   

然后用两种方式调用:
一:var arr= test();
console.log(arr.length);结果是4
arr.f1();
arr= test();
console.log(arr.length);结果是4
这个好理解;

二:var arr= test();
console.log(arr.length);结果是4
arr.f2();
arr= test();
console.log(arr.length);结果是0
不知道为什么会这样,难道arr.f2()可以修改test函数中闭包中的arr2;

以上就是【javascript 闭包疑问】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)

与本文相关的软件

发表我的评论

最新评论

  1. 暂无评论