js 深度拷贝递归生成

function deepClone(param){
   //判断时间
     if(param instanceof date) return new Date(param);
    //如果普通类型直接返回
    if(typeof param !== 'object') return param;

    //如果是则直接回调当前函数进行分解
    let cloneTarget = new param.constructor;
    for( let key in param){
        cloneTarget[key] = deepClone(param[key]);
    }

    return cloneTarget;
    
}


let obj = {
    name:'taotao',
    age:10,
    address:{
        city:'bj',
        living:{
            novers:true,
            time:10
        }
    
    }
}

let newObj = deepClone(obj)
console.log(newObj)

 

posted on 2022-11-24 13:01  totau  阅读(21)  评论(0编辑  收藏  举报

导航