There are a lot of discussions about this topic on the Internet. I sorted them out myself according to various situations. In the end, I was able to achieve deep copy almost perfectly. Everyone is welcome to discuss. Objects in 1. Direct assignmentAn object is a reference type. If it is directly assigned to another object, it is just a reference. In fact, the two variables point to the same data object. If the properties of one object change, the other one will also change. Example 1, simple example: let human1 = { id: 1, name: "happy" }; human2 = human1; // Here is a direct assignment console.log(human1); // {id: 1, name: 'happy'} console.log(human2); // {id: 1, name: 'happy'} // Changing human1's name will also change human2's human1.name = "life"; console.log(human1); // {id: 1, name: 'life'} console.log(human2); // {id: 1, name: 'life'} Example 2: passing an object as a parameter also passes a reference: let human1 = { id: 1, name: "happy" }; console.log(human1); // {id: 1, name: 'happy'} function foo(human) { // The name of the human object is changed here human.name = "life"; } foo(human1); // passing an object is by reference console.log(human1); // {id: 1, name: 'life'} 2. Shallow CopyA shallow copy only copies the first layer of the object. If the property value of the first layer is an object, then only a reference to the property is copied. let object1 = { a: 1, b: { // b is an object b1: 2 } }; object2 = Object.assign({}, object1); // This is a shallow copy, where only the reference of object b is copied // a is a regular type and will not affect each other object1.a = 10; console.log(object1.a); // 10 console.log(object2.a); // 1 // b is an object, which will affect each other object1.b.b1 = 20; console.log(object1.b.b1); // 20 console.log(object2.b.b1); // 20 If you want to achieve a complete copy, you must use a deep copy. 3. Deep CopySen copy means that not only one layer needs to be copied, but also the layers inside if they are objects need to be copied. 1. JSON object method If the object can be confirmed to be a Using the example above: let object1 = { a: 1, b: { // b is an object b1: 2 } }; object2 = JSON.parse(JSON.stringify(object1)); // Deep copy // a is a regular type and will not affect each other object1.a = 10; console.log(object1.a); // 10 console.log(object2.a); // 1 // b is an object and will not affect each other object1.b.b1 = 20; console.log(object1.b.b1); // 20 console.log(object2.b.b1); // 2 The principle of deep copy here is actually to convert the object into a
Therefore, this method is only suitable for objects that are confirmed to be pure 2. Recursive copyBecause we need to copy layer by layer, it is easy to think of using a recursive approach, refer to the following implementation: function deepCopy(source) { // If it is not an object or null, return directly if (typeof source !== 'object' || source === null) { return source; } let target = {}; // Traverse and copy properties for (let k in source) { if (!source.hasOwnProperty(k)) { continue; } if (typeof source[k] === 'object') { // If it is an object, recursively copy target[k] = deepCopy(source[k]); continue; } let descriptor = Object.getOwnPropertyDescriptor(source, k); Object.defineProperty(target, k, descriptor); } return target; } Because the objects are copied layer by layer, the two objects will not affect each other after the copying is completed, and methods can also be supported. let object1 = { a: 1, b: { // b is an object b1: 2 }, f: function() { // f is a method console.log(3); } }; object2 = deepCopy(object1); // Deep copy, you can also copy functions. object1.f(); // 3 object2.f(); // 3 // b is an object and will not affect each other object1.b.b1 = 20; console.log(object1.b.b1); // 20 console.log(object2.b.b1); // 2 Copying prototype objects But there is still a problem with this method, that is, the prototype object cannot be copied. Let's improve it a little bit: // Change let target = {}; to the following // to ensure that the prototype is also copied let target = Object.create(Object.getPrototypeOf(source)); That's it, let's verify it with an example: function Human() { this.id = 1; } Human.prototype.bar = function() { console.log("bar"); }; let human1 = new Human(); human2 = deepCopy(human1); console.log("human1", human1); console.log("human2", human2); Look at the prototypes of the next two objects: Deep copy the prototype object: Perfect copy. Of course, there is a problem with this method. If the recursion level is too deep, it may easily cause stack overflow. However, in practice it is also recommended not to copy very large objects, there should be other good solutions. This is the end of this article about Reference Documents: JS implements deep copy: https://www.cnblogs.com/dobeco/p/11295316.html You may also be interested in:
|
<<: MySQL infobright installation steps
>>: Five solutions to cross-browser problems (summary)
1. Disconnection reason There are many reasons wh...
Table of contents 1. Integrate Ant Design Vue 2. ...
I recently upgraded MySQL to 5.7, and WordPress r...
A certain distance can be set between cells in a ...
Table of contents Introduction Get started A brie...
This article uses examples to illustrate the prin...
Modern browsers no longer allow JavaScript to be ...
Previously, I introduced several ways to achieve ...
During the installation of Ubuntu 18, the mmx64.e...
origin Recently, I am working on requirement A, i...
Here we mainly use spring-boot out of the box, wh...
In SQL, GROUP BY is used to group data in the res...
To achieve this effect, you must first know a pro...
Table of contents 1. Introduction to Portainer 2....
As the most commonly used layout element, DIV play...