Deep copy and shallow copy
What are deep and shallow copies?
- Old saying cloud: old saying cloud: a b learn in the teacher, a learn good outside, B learn good inside. Division test its effect, a learning such as division, is that shallow learning; B learning more than division and independent, is called deep learning.
- In simple terms, B assigns data to A, A changes, B also changes, called shallow copy; If A changes and B does not change, it is called A deep copy.
1 Copy object
In general, shallow copy is for reference data types, such as objects, arrays, etc.
Data is divided into basic data type and reference data type. Basic data type data is stored in stack memory. Reference data types Data is stored in heap memory and only holds the start address opened in heap memory in occupied memory.
2 the difference between
- Why use deep copy?
- You want to change the new array without changing the original array.
- About Copy Hierarchy
- Shallow copy is a copy of a layer, deep object level copy reference;
- Deep copy is the copying of multiple layers, each level of data is copied;
2.1 shallow copy
For shallow copies, if the copied object is a primitive data type, copying is equivalent to direct assignment. If the object to be copied has several layers of objects in addition to the basic data type, then only the reference address of the object can be copied, and the value of the copy changes as the object changes.
- Shallow copy copies only Pointers to an object, not the object itself, and old and new objects still share the same block of memory (branching).
- A shallow copy is a bitwise copy of an object that creates a new object with an exact copy of the original object’s property values.
- If the property is of a primitive type, the value of the primitive type is copied. If the property is a memory address (reference type), the memory address is copied, so if one object changes the address, the other object will be affected.
2.2 Implementation of shallow copy
-
Object.assign()
Object.assign(target, ... sources)//target: indicates the target object //sources: any multiple source objects. // Return value: The target object is returned. Copy the code
- Method can copy any number of the source object’s own enumerable properties to the target object and then return the target object. But object.assign () makes a shallow copy, copying references to the Object’s attributes rather than the Object itself.
- When an object has only one layer, it is a deep copy
-
Array.prototype.concat()
- Modifying a new object changes to the original object
-
Array.prototype.slice()
- Modifying a new object changes to the original object
- Array’s slice and concat methods do not modify the Array; they simply return a new Array with a shallow copy of the elements in the Array
2.3 deep copy
- Deep copy creates an identical object. The new object does not share memory with the original object. Modifying the new object does not change to the original object.
- Copy the first level of object properties or array elements
- Recursively copy all levels of object properties and array elements
- Deep copy copies all attributes and dynamically allocated memory to which the attributes point. Deep copy occurs when an object is copied along with the object it references. Deep copy is slower and more expensive than shallow copy.
2.4 Implementation of deep copy
- JSON.parse(JSON.stringify())
- Parse () converts an object into a JSON string using json.stringify, and parses the string into an object using json.parse (). As you go, new objects are created, and the object opens up a new stack for deep copy.
- You can implement deep copies of arrays or objects, but not functions. Because the json.stringify () method does not accept functions.
- Data structures that can be represented directly by JSON. RegExp objects are not deep-copied in this way
- Handwritten recursive method
- Deep copy iterates through objects and arrays until they are full of primitive data types, and then copies them.
- Function library lodash
- The library provides _. CloneDeep for Deep Copy.
- Object. The create () method
- Use var newObj = object.create (oldObj) to create a deep copy.