Deep clone sao chép đệ quy toàn bộ nested values mà không giữ reference gốc.
js
function deepClone(obj) {
if (obj === null || typeof obj !== 'object') return obj;
const clone = Array.isArray(obj) ? [] : {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) clone[key] = deepClone(obj[key]);
}
return clone;
}Lưu ý: không handle Date, RegExp, Map, Set, circular refs.
Production dùng structuredClone() hoặc lodash.
Recursive approach: function deepClone(obj) { if (obj === null || typeof obj !== 'object') return obj; const clone = Array.isArray(obj) ? [] : {}; for (const key in obj) { if (obj.hasOwnProperty(key)) clone[key] = deepClone(obj[key]); } return clone; }
Note: does not handle Date, RegExp, Map, Set, or circular references. In production, use structuredClone() or lodash.