码迷,mamicode.com
首页 > Web开发 > 详细

js 的深拷贝

时间:2018-09-20 21:54:34      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:for   return   type   https   循环   code   console   str   func   

出处: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));

js 的深拷贝

标签:for   return   type   https   循环   code   console   str   func   

原文地址:https://www.cnblogs.com/cag2050/p/9683468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!