1. What does shallow copy mean?A shallow copy is a copy of only the references to the data stored in the stack, without copying the references pointing to the contents in the heap. Shallow copying of multiple data copies multiple references, which point to the same content in the heap. When a shallow copy of data is modified, that is, the content pointed to by the reference in the heap is modified, then other data pointed to here by reference will also change. let obj = { a:1, b:2, c:{ c1:10, c2:20 } } let objA = obj; objA.a = 'a'; console.log(obj.a); // 'a' console.log(objA.a); // 'a' 2. What does deep copy mean?A deep copy is a copy of the contents of the heap to create a new object. Multiple deep copies will be multiple different objects, which means they have different references and point to different heap contents. 3. Reasons for using deep copy In normal development, data is sometimes transmitted and received. After receiving the transmitted data, it is inevitable to process and transform the data. In order not to destroy the original data structure, deep copy can be used to copy the data, and then process the generated new data. Deep copying can also prevent the problem of reference confusion after modifying multiple references, reducing the chance of 4. Several methods for deep copyingImplementation method 1: JSON serialization and deserializationlet obj = { a:1, b:2, c:{ c1:10, c2:20 } } let objA = JSON.parse(JSON.stringify(obj)); //JSON serialization and deserialization objA.a = 'a'; console.log(obj.a); // 1 console.log(objA.a); // 'a' Although
let d1 = new Date(); let obj = { d1, d2: undefined, d3:NaN } let objD = JSON.parse(JSON.stringify(obj)); console.log(obj) console.log(objD) Implementation method 2: Object.assign()let obj = { a:1, b:2, c:{ c1:10, c2:20 } } let objA = Object.assign(obj); objA.a = 'a'; console.log(obj.a); // 1 console.log(objA.a); // 'a' Although let obj = { a:1, b:2, c:{ c1:10, c2:20 } } let objA = Object.assign(obj); objA.c.c1 = 'c1'; //Object.assign() is just a deep copy. console.log(obj.c.c1); // 'c1' console.log(objA.c.c1); // 'c1' Implementation method three: extension operatorlet obj = { a:1, b:2, c:{ c1:10, c2:20 } } let objA = {...obj};; objA.a = 'a'; console.log(obj.a); // 1 console.log(objA.a); // 'a' Although the spread operator " let obj = { a:1, b:2, c:{ c1:10, c2:20 } } let objA = {...obj}; objA.c.c1 = 'c1'; //The extension operator "..." is the same as Object.assign(), which only has one layer of deep copy and cannot have multiple layers of deep copy. console.log(obj.c.c1); // 'c1' console.log(objA.c.c1); // 'c1' Implementation method 4: Using recursionIf you want to achieve deep copying and multi-layer deep copying, you can use recursive loop copying. let obj = { a:1, b:2, c:{ c1:10, c2:20 } } const ReCopy = function (paramter) { let target = null; let isObject = paramter.constructor === Object; let isArray = paramter.constructor === Array; if (isObject || isArray) { target = Array.isArray(paramter) ? [] : {}; for (let i in parameter) { target[i] = ReCopy(paramter[i]); } } else { target = parameter; } return target; } let objA = ReCopy(obj); objA.c.c1 = 'c1'; console.log(obj.c.c1); // 10 console.log(objA.c.c1); // 'c1' 5. Ladash deep copy Install lodash Initialize first, generate the npm i -S lodash Import lodash var _ = require('lodash'); Using lodash let obj = { a:1, b:2, c:{ c1:10, c2:20 } } let objA = _.cloneDeep(obj); objA.c.c1 = 'c1'; console.log(obj.c.c1); // 10 console.log(objA.c.c1); // 'c1' This is the end of this detailed article about the deep and shallow copy of JS. For more relevant content about the deep and shallow copy of JS, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Introduction to the use of base link tag base
>>: MySQL account password modification method (summary)
Table of contents In vue2 In vue3 Notes on setup ...
After setting the table width in the page to width...
What is a memory leak? A memory leak means that a...
Table of contents TypeScript environment construc...
A few days ago, I watched a video of a foreign gu...
filter is generally used to filter certain values...
This article example shares the specific code of ...
Effect The effect diagram is as follows Implement...
Table of contents Target Thought Analysis Code la...
To query two different tables, you need to merge ...
(When a web page is loading, sometimes there is t...
Preface The project has requirements for charts, ...
When the submenu of the navigation bar is generat...
Make a note so you can come back and check it lat...
Install Oracle_11g with Docker 1. Pull the oracle...