美文网首页
递归实现对象深拷贝

递归实现对象深拷贝

作者: 清汤饺子 | 来源:发表于2018-09-12 10:14 被阅读0次

ex:递归实现对象深拷贝

function deepCopy(target,source){
    for(let index in source){
        if(typeof source[index] === "object"){
            target[index] = {};
            deepCopy(target[index],source[index])
        }else{
            target[index] = source[index];
        }
    }
}
let b = {a:1,d:{b:1}};
let a = {};
deepCopy(a,b);
 a.d.b = 4;
console.log(b)`

相关文章

网友评论

      本文标题:递归实现对象深拷贝

      本文链接:https://www.haomeiwen.com/subject/oxwmgftx.html