其他分享
首页 > 其他分享> > 手写深拷贝

手写深拷贝

作者:互联网

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 )

  

标签:instanceof,function,obj,copyRight,let,key,手写,拷贝
来源: https://www.cnblogs.com/uimeigui/p/14311738.html