SOURCE

function deepClone(data) {
    if(!isObject(data)) return data;
    const newData = Array.isArray(data) ? [] : {}
    // 通过构造函数直接创建
    // const newData = new data.constructor;
    for (let i in data) {
        if (data.hasOwnProperty(i)) {
            newData[i] = deepClone(data[i])
        }
    }
    // 写法二
    // Object.keys(data).map(i => {
    //     newData[i] = deepClone(data[i])
    // })
    return newData
}

function isObject(data) {
    return typeof data === 'object' && data !== null
}

const m = { x: 1, y: [1, 3, null, function () { }] };
const n = deepClone(m);
n.x = 0
console.log(n)
console.log(m)
console 命令行工具 X clear

                    
>
console