手写深拷贝

let o = {}
let obj = {
    a: 1,
    arr: [1, 2],
    obj: {
        c: 1
    },
    func: function () {
        console.log(123)
    }
}

function copyRight(d, r) {
    for (let key in d) {
        if (d[key] instanceof Object && !(d[key] instanceof Array) && !(d[key] instanceof Function)) {
            r[key] = {}
            copyRight(d[key], r[key])
        } else if (d[key] instanceof Array) {
            r[key] = []
            copyRight(d[key], r[key])
        } else {
            r[key] = d[key]
        }
    }
}

copyRight(obj, o)
console.log(o )

  

上一篇:CentOS8中安装php7与php5并同时运行


下一篇:Python study----------copyright()