xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

obj deep clone generator function

obj deep clone generator function

no need lodash deep clone

import _ from 'lodash';


const creativeBoxObj = {
    creative_box_name: '',
    creative_ids_map: [],
    resourcePositions: [],
    smartFrame: false,
    materialMining: false,
};


        addCreativeBox () {
            this.ruleForm.creativeBoxList.push({
                ..._.cloneDeep(creativeBoxObj),
            });
        },

obj = {k: 3, a: [1]};
{k: 3, a: Array(1)}
test = {...obj};
{k: 3, a: Array(1)}
test.key = 4;
4
obj;
{k: 3, a: Array(1)}
test.a.push(5);
2
test;
{k: 3, a: Array(2), key: 4}
obj;
{k: 3, a: Array(2)}a: (2) [1, 5]k: 3__proto__: Object
function g () {
 const obj = {k: 3, a: [1]};
 console.log('obj', obj);
 return {...obj};
}
undefined


t = g();
VM18885:3 obj {k: 3, a: Array(1)}
{k: 3, a: Array(1)}
t.a.push(3);
2
t;
{k: 3, a: Array(2)}
g();
VM18885:3 obj {k: 3, a: Array(1)}
{k: 3, a: Array(1)}
function gg () {
 const obj = {k: 3, a: [1]};
 console.log('init obj', obj);
 return obj;
}
undefined
tt = gg();
VM19017:3 init obj {k: 3, a: Array(1)}
{k: 3, a: Array(1)}
tt.a.push(3);
2
tt;
{k: 3, a: Array(2)}
gg();
VM19017:3 init obj {k: 3, a: Array(1)}
{k: 3, a: Array(1)}

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-07-12 16:41  xgqfrms  阅读(38)  评论(0编辑  收藏  举报