手动实现简单的深度拷贝

function copyDeep(obj){
        if (typeof obj === 'object'&& obj!=null) {
            let target = Array.isArray(obj)?[]:{};
            for(let i in obj){
                if(Object.prototype.hasOwnProperty.call(obj,i)){
                    if(typeof obj === 'object'&& obj!=null){
                        target[i]=copyDeep(obj[i])
                    } else {
                        target[i]=obj[i]
                    }
                }
            }
            return target;
        } else {
            return obj;
        }
    }

 

posted @ 2019-10-28 17:07  王红娇  阅读(160)  评论(0编辑  收藏  举报