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 APIs used Simple Example person...
Under Ubuntu 18.04 1. sudo apt install python ins...
As usual, today I will talk about a very practica...
Tomcat is a web server software based on Java lan...
Table of contents 1. Introduction 2. Scenario 3. ...
Table of contents What is Express middleware? Req...
What is HTML? HTML is a language used to describe...
MySQL advantage: Small size, fast speed, low tota...
When developing and debugging a web application, ...
What is HTTP? When we want to browse a website, w...
The installation tutorial of mysql 5.7.19 winx64 ...
All the orchestration files and configuration fil...
1. CSS3 animation ☺CSS3 animations are much easie...
The main part of the page: <body> <ul id...
Document hints using the show-header attribute sh...