Before understanding the depth of JS copy, I think it is necessary to mention about value passing and reference passing.
In JS, the copy of the base type value is passed by value, while the copy of the reference type value is passed by reference. Value-passing copied objects are not implicated, independent of each other, but the objects that refer to replication are affected by each other, and the values of either of them are reflected in the other party. The reason for this performance and JS memory mechanism.
JS memory is also divided into heaps and stacks, note here the stack and data structure of the stack are different concepts.
Stack: Automatically allocated by the system, automatic recovery, high efficiency, but small capacity
Heap: Manually allocated memory by the programmer, and manually destroyed (high-level language such as JS in the garbage automatic recovery mechanism), efficiency than the stack, but large capacity
The base type value of the JS definition is stored in the stack, and because the reference type is not fixed, the system allocates the heap memory space for the reference type, and only the pointer (that is, the reference) that points to the heap memory space is stored on the stack. In this way, when we access the reference type value, we essentially simply access its reference and then follow the reference address to find its actual contents in the heap.
So when copying, for a primitive type value variable, the system opens up a new stack memory space for the new variable, and copies the value of the source variable to the inside. For reference-type values, the new variable replicates only the memory address of the referenced object, so that the reference to the same reference type object is accessed by referencing the two variables. That's why a modification of a reference type value on one side causes a change in the value of the reference type that is accessed by the other party.
Shallow copy refers to copying only the first-level key value of an object when copying an object.
In the above example, if you do not want to modify the name value of the New_person object, the name value of the source object is changed along with it, then we can try to do some processing of the copy process instead of being a direct copy of the assignment.
When we modify the name value of the copied object, the name of the source object is no longer changed, but when we modify the value of the property sport, the sport of the source object is changed.
As we said earlier, our shallow copy is just a copy of the first layer of key values, and when other objects are nested inside the source object, there will be a situation at the beginning. The Love property of the copied object replicates the address of the object to which the Love property of the source object is, so that the love attribute of the copied object and the source object points to the same memory address in the heap memory. The Object.assign () method is also a shallow copy of the implementation. So how do you make a copy of it completely independent? In fact, as long as the recursive, the value of the internal property is still the object's entry inside the object to its property value one by one copy. Object deep copy can also be implemented with JSON. However, a deep copy implemented in this way ignores attributes that are values undefined and function expressions.
Javascript shallow copy and deep copy