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
Q1: What indexes does the database have? What are...
Table of contents 1. Introduction 2. Installation...
Insert image tag <IMG> The colorful web page...
Table of contents background Server Dependencies ...
This tutorial shares the installation and configu...
The implementation principle of Vue2.0/3.0 two-wa...
Several parts of Compose deal with environment va...
1. Background During the server development proce...
This article example shares the specific code for...
Table of contents background analyze Data simulat...
How to define complex components (class component...
1. Import csv file Use the following command: 1.m...
introduction The company's recent Vue front-e...
In fact, there are many corresponding writing met...
Preface In JavaScript, this is the function calli...