递归实现对象深拷贝

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)`

你可能感兴趣的:(递归实现对象深拷贝)