Shallow clone vs Deep clone - quan1997ap/angular-app-note GitHub Wiki
Link: https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy
Shallow clone: Chỉ sao chép đối tượng nhưng giữ tham chiếu
Deep clone: Tạo một bản sao mới với một tham chiếu mới\
Ví dụ
const obj = { ... };
Shallow copy Note In JavaScript, standard built-in object-copy operations (spread syntax, Array.prototype.concat(), Array.prototype.slice(), Array.from(), Object.assign(), and Object.create()) do not create deep copies (instead, they create shallow copies).
const shallow = obj;
Deep copy
// Using structuredClone
const clone = structuredClone(original);
// Using JSON.parse & JSON.stringify
JSON.parse(JSON.stringify(obj));
// Using Object.assign. Chi deep copy level dau tien cua object. Doc chu y ben tren
const deep = Object.assign({}, obj);
// Using object spread. Chi deep copy level dau tien cua object. Doc chu y ben tren
const deep = { ...obj };