javascript对象的深度克隆/复制

javascript对象的深度克隆/复制,javascript,object,clone,Javascript,Object,Clone,为了寻找深度复制包含嵌套和循环结构的对象的方法,当涉及到循环引用和原型继承时,这两个(,)都没有完美的解决方案 我在这里写了我自己的。它能很好地完成工作还是算作更好的解决方案 /* a function for deep cloning objects that contains other nested objects and circular structures. objects are stored in a 3D array, according to their l

为了寻找深度复制包含嵌套和循环结构的对象的方法,当涉及到循环引用和原型继承时,这两个(,)都没有完美的解决方案

我在这里写了我自己的。它能很好地完成工作还是算作更好的解决方案

 /*
    a function for deep cloning objects that contains other nested objects and circular structures.
    objects are stored in a 3D array, according to their length (number of properties) and their depth in the original object.
                                    index (z)
                                         |
                                         |
                                         |
                                         |
                                         |
                                         |                      depth (x)
                                         |_ _ _ _ _ _ _ _ _ _ _ _
                                        /_/_/_/_/_/_/_/_/_/
                                       /_/_/_/_/_/_/_/_/_/
                                      /_/_/_/_/_/_/...../
                                     /................./
                                    /.....            /
                                   /                 /
                                  /------------------
            object length (y)    /
*/


function deepClone(obj) {
    var i = -1, //depth of the current object relative to the passed 'obj'
        j = 0;  //number of the object's properties
    var arr = new Array(); //3D array to store the references to objects
    return clone(obj, arr, i, j);
}

function clone(obj, arr, i ,j){
    if (typeof obj !== "object") {
        return obj;
    }

    var result = Object.create(Object.getPrototypeOf(obj)); //inherit the prototype of the original object
    if(result instanceof Array){
        result.length = Object.keys(obj).length;
    }

    i++; //depth is increased because we entered an object here
    j = Object.keys(obj).length; //native method to get the number of properties in 'obj'
    arr[i] = new Array(); //this is the x-axis, each index here is the depth
    arr[i][j] = new Array(); //this is the y-axis, each index is the length of the object (aka number of props)
    //start the depth at current and go down, cyclic structures won't form on depths more than the current one
    for(var depth = i; depth >= 0; depth--){
        //loop only if the array at this depth and length already have elements
        if(arr[depth][j]){
            for(var index = 0; index < arr[depth][j].length; index++){
                if(obj === arr[depth][j][index]){
                    return obj;
                }
            } 
        }
    }

    arr[i][j].push(obj); //store the object in the array at the current depth and length
    for (var prop in obj) {
        result[prop] = clone(obj[prop], arr, i, j);
    }

    return result;
}
/*
用于深度克隆包含其他嵌套对象和循环结构的对象的函数。
根据对象在原始对象中的长度(特性数量)和深度,对象存储在三维阵列中。
指数(z)
|
|
|
|
|
|深度(x)
|_ _ _ _ _ _ _ _ _ _ _ _
/_/_/_/_/_/_/_/_/_/
/_/_/_/_/_/_/_/_/_/
/_/_/_/_/_/_/...../
/................./
/.....            /
/                 /
/------------------
对象长度(y)/
*/
功能克隆(obj){
var i=-1,//当前对象相对于传递的“obj”的深度
j=0;//对象的属性数
var arr=new Array();//用于存储对象引用的3D数组
返回克隆(obj、arr、i、j);
}
功能克隆(obj、arr、i、j){
if(对象类型!=“对象”){
返回obj;
}
var result=Object.create(Object.getPrototypeOf(obj));//继承原始对象的原型
if(数组的结果实例){
result.length=Object.keys(obj).length;
}
i++;//因为我们在这里输入了一个对象,所以深度增加了
j=Object.keys(obj.length;//获取“obj”中属性数的本机方法
arr[i]=new Array();//这是x轴,这里的每个索引都是深度
arr[i][j]=new Array();//这是y轴,每个索引都是对象的长度(也称为道具数)
//从当前深度开始向下,循环结构不会在超过当前深度的深度上形成
对于(变量深度=i;深度>=0;深度--){
//仅当此深度和长度的数组已包含元素时循环
if(arr[深度][j]){
对于(var index=0;index
你能试试下面的简单逻辑吗。将js对象转换为新字符串,然后再次转换为对象

例如:


你试过这个吗?@Ja͢ck以前看过一眼。我相信它不处理循环结构,对吗?另外,您需要手动实现特殊情况(例如日期、数组等)。在描述结构化克隆算法的页面的第一段提到循环图。基本上,这个问题是研究得很好的图遍历问题。@akonsu我正在阅读它的伪代码。你认为哪一个更快?3D阵列,当我接近它时,还是地图,当他们接近它时?
var car = {type:"Fiat", model:"500", color:"white"};
var cloneStr = new String(JSON.stringify(car));
var carClone = JSON.parse(cloneStr);
carClone.type = "Maruthi";
alert(JSON.stringify(car)) ;
alert(JSON.stringify(carClone)) ;