SOURCE

console 命令行工具 X clear

                    
>
console
function checkType(obj){
    return Object.prototype.toString.call(obj).slice(8,-1)
}

function deepCopy(target, hash = new WeakMap()){
    // 获取参数类型
    let type = checkType(target)
    let res;
    if(type === 'Object') res = {}
    else if (type === 'Array') res = []
    else return target

    if(hash.has(target)) return hash.get(target)
    hash.set(target, res)

    for (const key in target) {
        res[key] = deepCopy(target[key], hash)
    }
    return res
}

const obj = {
    foo: {
        azz: [1, 3, 2]
    },
    age: [1, {
        baz: 71
    }],
    num: 3
}
let objc = deepCopy(obj);
console.log(obj)
console.log(objc)
console.log(objc === obj)
<div>请查看控制台</div>