1. Shallow copy1. Object.assign(target,source,source...)a. Supports multiple object replication b. If the source and target attributes are the same, the source will copy the target attributes c. Target can only be an Object object var obj = {a:1,b:2} undefined Object.assign({c:3},obj) {c: 3, a: 1, b: 2} obj {a: 1, b: 2} Compatibility writing if (Object.assign) {//compatible} else {//incompatible} 2. Spread operatorSupports copying multiple objects to one object" var obj1 = { foo: "foo" }; var obj2 = { bar: "bar" }; var copySpread = { ...obj1, ...obj2 }; // Object {foo: "foo", bar: "bar"} copySpread {foo: "foo", bar: "bar"} var obj = {a:1,b:2,c:3} var objs = {...obj} objs {a: 1, b: 2, c: 3} objs.a=10 10 objs {a: 10, b: 2, c: 3} obj {a: 1, b: 2, c: 3} 2. Deep Copy1. Use object serialization JSON.stringify() and JSON.parse()Note: This method only works if the original object contains serializable value types and does not have any circular references. An example of a non-serializable value type is a Date object - JSON.parse can only parse it into a string and cannot parse it back into its original Date object or an object whose property value is a function. var obj = {a:1,b:[1,2,3],c:{e:3},bool:false} undefined var objs = JSON.parse(JSON.stringify(obj)) undefined objs {a: 1, b: Array(3), c: {…}, bool: false} objs.bool = true true objs {a: 1, b: Array(3), c: {…}, bool: true} obj {a: 1, b: Array(3), c: {…}, bool: false} 2. Use recursion to judge object propertiesfunction deepClone(obj) { var copy; // If obj is null, undefined or not an object, return obj directly // Handle the 3 simple types, and null or undefined if (null == obj || "object" != typeof obj) return obj; // Handle Date if (obj instanceof Date) { copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { copy = []; for (var i = 0, len = obj.length; i < len; i++) { copy[i] = clone(obj[i]); } return copy; } //Handle Function if (obj instanceof Function) { copy = function() { return obj.apply(this, arguments); } return copy; } //Handle Object if (obj instanceof Object) { copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]); } return copy; } throw new Error("Unable to copy obj as type isn't supported " + obj.constructor.name); } The above is the details of JS object copying (deep copy and shallow copy). For more information about JS, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Command to view binlog file creation time in Linux
>>: Summary of Linux user groups and permissions
Table of contents Preface application Filters Dra...
Whether it is Samba service or NFS service, the m...
The span tag is often used when making HTML web p...
Table of contents 1. Background 2. What is a virt...
Problem to be solved Mainly for cross-level commu...
1. Requirements: Database backup is particularly ...
Table of contents Boot Options Command Line Long ...
I have used the vi editor for several years, but ...
Preface This article mainly introduces the releva...
This article example shares the specific code of ...
Linux file permissions First, let's check the...
1. Confirm whether MySQL has been installed. You ...
Introduction: This article takes the sample proje...
This article example shares the specific code of ...
Today I encountered a very strange situation. Aft...