当前位置:网站首页>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;} }
边栏推荐
- Interrupt sharing variables with other functions and protection of critical resources
- Which method is good for the management of fixed assets of small and medium-sized enterprises?
- 大型工厂设备管理痛点和解决方案
- Shell脚本-数组定义以及获取数组元素
- Serial port to WiFi module communication
- Shell script case in statement
- 中小企业固定资产管理办法哪种好?
- 小鸟识别APP
- 5mo3 UHI HII HII 17mn4 19Mn6 executive standard
- Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
猜你喜欢
Principle and application of single chip microcomputer - off chip development
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
Guidelines and principles of did
公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度
Do you know how data is stored? (C integer and floating point)
Glitch Free时钟切换技术
FreeRTOS learning easy notes
Screenshot tips
如何解决固定资产管理和盘点的难题?
[untitled]
随机推荐
Software Engineer Interview Question brushing website and experience method
Glitch Free时钟切换技术
电视机尺寸与观看距离
1. Connection between Jetson and camera
Nacos - 服务发现
[untitled]
Shell script -read command: read data entered from the keyboard
Shell script -if else statement
Serial port to WiFi module communication
Shell脚本-数组定义以及获取数组元素
Shell脚本-case in 和正则表达式
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
The fixed assets management system enables enterprises to dynamically master assets
Nacos - 配置管理
Shell script - special variables: shell $, $*, [email protected], $$$
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
电脑小技巧
Shell script case in and regular expressions
足球篮球体育比赛比分直播平台源码/app开发建设项目
如何做好固定资产管理?易点易动提供智能化方案