javascript对象的深拷贝

时间:2022-07-07 19:52:24
其实我的办法并不是很好,不过好歹是自己琢磨的, javascript对象的深拷贝,最开始的思路也是来自网上,就是不区分数组和类,统一只拷贝属性,但我在使用中发现如果拷贝对象是数组或者类的某个属性是数组的话,拷贝过去之后的数组全都无法用下标去访问了,于是我就简单的跟踪了下,发现简单的for each是不能拷贝Object本身的属性的,就是说,你new一个Object,然后给这个Object添加属性,然后用for each的方式去访问这个对象的属性的话,只能访问到后来添加的属性,而数组显然和Object不一样,所以仅仅只拷贝Object的方法就不能适应Array了,于是有了下面的代码:
javascript对象的深拷贝function Clone(original)
javascript对象的深拷贝javascript对象的深拷贝
{
javascript对象的深拷贝    
if(typeof(original) == "object"
javascript对象的深拷贝javascript对象的深拷贝    
{
javascript对象的深拷贝        
if (original.length != undefined)
javascript对象的深拷贝javascript对象的深拷贝        
{
javascript对象的深拷贝            
return CloneArray(original);
javascript对象的深拷贝        }

javascript对象的深拷贝        
javascript对象的深拷贝        
return CloneObject(original);
javascript对象的深拷贝    }

javascript对象的深拷贝    
javascript对象的深拷贝    
return original;
javascript对象的深拷贝}

javascript对象的深拷贝
javascript对象的深拷贝function CloneObject(original)
javascript对象的深拷贝javascript对象的深拷贝
{
javascript对象的深拷贝    var newObj 
= new Object();
javascript对象的深拷贝    
javascript对象的深拷贝    
for(var ele in original)
javascript对象的深拷贝javascript对象的深拷贝    
{
javascript对象的深拷贝        
if(typeof(original[ele]) == "object"
javascript对象的深拷贝javascript对象的深拷贝        
{
javascript对象的深拷贝            
if (original[ele].length != undefined)
javascript对象的深拷贝javascript对象的深拷贝            
{
javascript对象的深拷贝                 newObj[ele] 
= CloneArray(original[ele]);
javascript对象的深拷贝            }

javascript对象的深拷贝            
else
javascript对象的深拷贝javascript对象的深拷贝            
{
javascript对象的深拷贝                newObj[ele] 
= CloneObject(original[ele]);
javascript对象的深拷贝            }

javascript对象的深拷贝        }

javascript对象的深拷贝        
else
javascript对象的深拷贝javascript对象的深拷贝        
{
javascript对象的深拷贝            newObj[ele] 
= original[ele];
javascript对象的深拷贝        }

javascript对象的深拷贝    }

javascript对象的深拷贝    
javascript对象的深拷贝    
return newObj;
javascript对象的深拷贝}

javascript对象的深拷贝
javascript对象的深拷贝function CloneArray(original)
javascript对象的深拷贝javascript对象的深拷贝
{
javascript对象的深拷贝    var newArray 
= new Array();
javascript对象的深拷贝    
javascript对象的深拷贝    
for (var i = 0; i < original.length; i++)
javascript对象的深拷贝javascript对象的深拷贝    
{
javascript对象的深拷贝        
if(typeof(original[i]) == "object"
javascript对象的深拷贝javascript对象的深拷贝        
{
javascript对象的深拷贝            
if (original[i].length != undefined)
javascript对象的深拷贝javascript对象的深拷贝            
{
javascript对象的深拷贝                newArray[i] 
= CloneArray(original[i]);
javascript对象的深拷贝            }

javascript对象的深拷贝            
else
javascript对象的深拷贝javascript对象的深拷贝            
{
javascript对象的深拷贝                newArray[i] 
= CloneObject(original[i]);
javascript对象的深拷贝            }

javascript对象的深拷贝        }

javascript对象的深拷贝        
else
javascript对象的深拷贝javascript对象的深拷贝        
{
javascript对象的深拷贝            newArray[i] 
= original[i];
javascript对象的深拷贝        }

javascript对象的深拷贝    }

javascript对象的深拷贝    
javascript对象的深拷贝    
return newArray;
javascript对象的深拷贝}

这里我对怎么判断是不是数组没有找到好的办法。