js 的深拷贝

出处:https://www.cnblogs.com/Chen-XiaoJun/p/6217373.html

function deepClone(initalObj, finalObj) {
    var obj = finalObj || {};
    for (var i in initalObj) {
        var prop = initalObj[i];        // 避免相互引用对象导致死循环,如initalObj.a = initalObj的情况
        if (prop === obj) {
            continue;
        }
        if (typeof prop === 'object') {
            obj[i] = (prop.constructor === Array) ? prop : Object.create(prop);
        } else {
            obj[i] = prop;
        }
    }
    return obj;
}

var initalObj = {a: ['1', {b: 'bbb'}]}
console.log(deepClone(initalObj));

posted on 2018-09-20 21:32  cag2050  阅读(109)  评论(0编辑  收藏  举报

导航