SOURCE

let origin = {
    name: "张三",
    age: 18,
    sub: {
        subText: "语文",
        subNumber: "100"
    },
    love: ['数学', '英语', '体育', '美术']
}
function deepClone(origin) {
    //刨除数组和对象和null之外的所有类型
    if (typeof origin !== "object" || origin === null) {
        return origin
    }
    let cloneObj;
    if (Array.isArray(origin)) {
        cloneObj = []
    } else {
        cloneObj = {}
    }
    for (let key in origin) {
        //只拷贝对象自身的属性,不拷贝原型属性
        if (origin.hasOwnProperty(key)) {
            cloneObj[key] = deepClone(origin[key])
        }

    }
    return cloneObj
}

const cloneObj = deepClone(origin)
cloneObj.name = "李四"
cloneObj.sub.subText = "物理"
cloneObj.love[3] = "音乐"
console.log("深拷贝:", cloneObj)
console.log("原对象:", origin)
console 命令行工具 X clear

                    
>
console