当前位置:网站首页>Understand shallow replication and deep replication through code examples
Understand shallow replication and deep replication through code examples
2022-07-01 08:52:00 【ylnzzl】
Catalog
explain
If the members of an object are all value types , Then you can use the extension operator or Object.assign To achieve deep replication . If the type of an object's member is other than the value type , Also contains reference types , So you can go through JSON.parse(JSON.stringify(obj)) Or recursively traverse the replication to achieve deep replication , among JSON.parse(JSON.stringify(obj)) In this way, the value of undefined and function Properties of , and JSON.parse Can only be Date The object resolves to a string and cannot be resolved to the original Date object .
Example 1
let obj1={a:1,b:"2",c:[1,2],d:{e:1,f:2}};
let obj2={...obj1};// perhaps let obj2={}; Object.assign(obj2,obj1);
obj2.b="3";
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "3", c: Array [1, 2], d: Object { e: 1, f: 2 } }
obj2={...obj1}; // perhaps obj2={}; Object.assign(obj2,obj1);
obj2.c=[2,3];
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "2", c: Array [2, 3], d: Object { e: 1, f: 2 } }
obj2={...obj1}; // perhaps obj2={}; Object.assign(obj2,obj1);
obj2.c[0]=0;
console.log(obj1); // Object { a: 1, b: "2", c: Array [0, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "2", c: Array [0, 2], d: Object { e: 1, f: 2 } }
obj1={a:1,b:"2",c:[1,2],d:{e:1,f:2}}; // Because the code above has changed obj1 Value , So here's a reset .
obj2={...obj1}; // perhaps obj2={}; Object.assign(obj2,obj1);
obj2.d={e:3};
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 3 } }
obj2={...obj1}; // perhaps obj2={}; Object.assign(obj2,obj1);
obj2.d.e=4;
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 4, f: 2 } }
console.log(obj2); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 4, f: 2 } }Example 2
let obj1={a:1,b:"2",c:[1,2],d:{e:1,f:2}};
let obj3={a:null,b:undefined,c:NaN,d:false,e:0,f:'',g:{e:1,f:2},h:new Date(),j:function(){return 1;}};
let obj2=JSON.parse(JSON.stringify(obj1));
obj2.b="3";
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "3", c: Array [1, 2], d: Object { e: 1, f: 2 } }
let obj4=JSON.parse(JSON.stringify(obj3)); // obj4 The middle shift value is undefined and function Properties of , And the value is NaN The value of the property of is converted to null. In addition, for a value of Date Object properties , Not copied as Date object .
obj4.g.e="3";
console.log(obj3); // Object { a: null, b: undefined, c: NaN, d: false, e: 0, f: "", g: Object { e: 1, f: 2 }, h: Mon Jun 13 2022 13:23:13 GMT+0800 ( China standard time ), j: function(){return 1;} }
console.log(obj4); // Object { a: null, c: null, d: false, e: 0, f: "", g: Object { e: "3", f: 2 }, h: "2022-06-14T11:39:11.716Z" }
obj2=JSON.parse(JSON.stringify(obj1));
obj2.c=[2,3];
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "2", c: Array [2, 3], d: Object { e: 1, f: 2 } }
obj2=JSON.parse(JSON.stringify(obj1));
obj2.c[0]=0;
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "2", c: Array [0, 2], d: Object { e: 1, f: 2 } }
obj2=JSON.parse(JSON.stringify(obj1));
obj2.d={e:3};
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 3 } }
obj2=JSON.parse(JSON.stringify(obj1));
obj2.d.e=4;
console.log(obj1); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 1, f: 2 } }
console.log(obj2); // Object { a: 1, b: "2", c: Array [1, 2], d: Object { e: 4, f: 2 } }Example 3
let obj1={b:1,a:function(){return 1},};
let obj2=obj1; // A shallow copy , When changing the copied object , Will change the original object .
obj2.a=function(){return 2};
console.log(obj1); // Object { b: 1, a: function(){return 2} }
console.log(obj2); // Object { b: 1, a: function(){return 2} }
obj2.b=2;
console.log(obj1); // Object { b: 2, a: function(){return 2} }
console.log(obj2); // Object { b: 2, a: function(){return 2} }
let obj3={b:1,a:function(){return 1},};
let obj4=obj3;
obj4={b:1,a:function(){return 2},}; // to obj4 After reassigning ,obj4 Just like obj3 It doesn't matter .
console.log(obj3); // Object { b: 1, a: function(){return 1} }
console.log(obj4); // Object { b: 1, a: function(){return 2} }
obj4.a=function(){return 3}
console.log(obj3); // Object { b: 1, a: function(){return 1} }
console.log(obj4); // Object { b: 1, a: function(){return 3} }
obj4.b=3;
console.log(obj3); // Object { b: 1, a: function(){return 1} }
console.log(obj4); // Object { b: 3, a: function(){return 3} }Example 4
let obja={a:null,b:undefined,c:NaN,d:false,e:0,f:'',g:{e:1,f:2},h:new Date('2022-06-11'),j:function(){return 1;}};
function deepClone(obj){
let newObj;
if(obj == null || "object" != typeof obj) return obj; // Notice that this is "object", instead of "Object".
if(obj instanceof Date){
newObj = new Date();
newObj.setTime(obj.getTime());
return newObj;
}
if(obj instanceof Array){
newObj = [];
let i;
for(i=0;i<obj.length;i++){
newObj[i]=deepClone(obj[i]); // The reason why it's not used here push, To facilitate iterative deep replication .
}
return newObj;
}
if(obj instanceof Function){
newObj=function(){
return obj.apply(this,arguments);
}
return newObj;
}
if(obj instanceof Object){
newObj={};
for(let item in obj){
if(obj.hasOwnProperty(item)){
newObj[item] = deepClone(obj[item]);
}
}
return newObj;
}
throw new Error(" Replicated object types are not supported :"+obj.constructor.name);
}
let objb=deepClone(obja); // Deep copy , Don't change the original object .
objb.h= new Date('2022-06-12');
console.log(obja); // Object { a: null, b: undefined, c: NaN, d: false, e: 0, f: "", g: Object { e: 1, f: 2 }, h: Sat Jun 11 2022 08:00:00 GMT+0800 ( China standard time ), j: function(){return 1;} }
console.log(objb); // Object { a: null, b: undefined, c: NaN, d: false, e: 0, f: "", g: Object { e: 1, f: 2 }, h: Sun Jun 12 2022 08:00:00 GMT+0800 ( China standard time ), j: function(){return 1;} }
objb.j=function(){return 2;}
console.log(obja); // Object { a: null, b: undefined, c: NaN, d: false, e: 0, f: "", g: Object { e: 1, f: 2 }, h: Sat Jun 11 2022 08:00:00 GMT+0800 ( China standard time ), j: function(){return 1;} }
console.log(objb); // Object { a: null, b: undefined, c: NaN, d: false, e: 0, f: "", g: Object { e: 1, f: 2 }, h: Sun Jun 12 2022 08:00:00 GMT+0800 ( China standard time ), j: function(){return 2;} }边栏推荐
- I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
- 内存大小端
- Shell script - positional parameters (command line parameters)
- Share 7 books I read in the first half of 2022
- 爬虫知识点总结
- 固定资产管理系统让企业动态掌握资产情况
- Memory size end
- Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
- FreeRTOS学习简易笔记
- Shell脚本-read命令:读取从键盘输入的数据
猜你喜欢

Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling

如何做好固定资产管理?易点易动提供智能化方案

19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition

安装Oracle EE

Which method is good for the management of fixed assets of small and medium-sized enterprises?

动态代理

What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR

大型工厂设备管理痛点和解决方案

为什么LTD独立站就是Web3.0网站!

截图小妙招
随机推荐
Shell script -if else statement
Embedded Engineer Interview Question 3 Hardware
Vscode customize the color of each area
Introduction to R language
[MFC development (17)] advanced list control list control
大型工厂设备管理痛点和解决方案
Shell script case in statement
1.jetson与摄像头的对接
嵌入式工程师常见面试题2-MCU_STM32
Shell script - array definition and getting array elements
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
Shell脚本-echo命令 转义符
Advanced level of C language pointer (Part 1)
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
TV size and viewing distance
Memory size end
Guidelines and principles of did
电脑小技巧
C语言指针的进阶(上篇)
集团公司固定资产管理的痛点和解决方案