深拷贝

function deepClone(obj = {}){
    if(typeof obj !== 'object' || obj == null){
        // 不是对象和数组 或者 是null
        return obj
    }

    let result
    if(obj instanceof Array){
        result = []
    }else{
        result = {}
    }

    for(let key in obj){
        // 保证 key 不是原型上的属性
        if(obj.hasOwnProperty(key)){
            // 递归
            result[key] = deepClone(obj[key])
        }
    }

    return result
}
posted @ 2020-08-13 16:37  大海博客  阅读(72)  评论(0)    收藏  举报