js配列深さクローン

3736 ワード

 1 var a =[],b=[];

 2 a[0]={name:'w',text:2};

 3 a[1]={name:'a',text:3};

 4 a[2]={name:'b',text:4};

 5 b =a.cloneArray();

 6 function objClone(myObj){

 7     if(typeof(myObj) != 'object') return myObj;

 8     if(myObj == null) return myObj;

 9     var myNewObj = new Object();

10     for(var i in myObj)

11     myNewObj[i] = objClone(myObj[i]);

12     return myNewObj;

13 }

14   Array.prototype.cloneArray=function(){//

15     var newArr=new Array();

16     for(var i=0;i<=this.length-1;i++)

17     {

18        var itemi=this[i];

19        if(itemi.length && itemi.push) itemi= itemi.cloneArray();//

20        else if(typeof(itemi)=="object") itemi=objClone(itemi);//     ,    objClone    

21        newArr.push(itemi);

22     }

23     return newArr;

24   }