javascript 闭包疑问

时间:2010-12-30    点击:79   
复制代码 代码如下:

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延迟执行实现方法(setTimeout)
JavaScript的document对象和window对象详解
javascript hashtable 修正版 下载
javascript中运用闭包和自执行函数解决大量的全局变量问题
javascript中创建对象的三种常用方法
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved