1. Shallow copy
Concept: Child objects copy parent objects, parent objects are associated, and both attribute values point to the same memory space. In simple terms, if you change one object, the other object will also change.
let a = [0.1.2],
b = a;
a[0] = 3;
console.log(a,b) / /,1,2 [3],1,2 [3]
Copy the code
2. Deep copy
Concept: Copy attributes at all levels of an object. Simply put, each object copied has its own memory space and does not interfere with each other.
function deepClone(obj) {
const objClone = Array.isArray(obj) ? [] : {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === "object") {
objClone[key] = deepClone(obj[key]);
} else{ objClone[key] = obj[key]; }}}return objClone;
}
Copy the code