[Medium] Deep Clone
1. What is Deep Clone?
什么是 Deep Clone?
**深拷贝(Deep Clone)**是指创建一个新对象,并且递归地复制原始对象及其所有嵌套对象和数组的所有属性。深拷贝后的对象与原始对象完全独立,修改其中一个不会影响另一个。
浅拷贝 vs 深拷贝
浅拷贝(Shallow Clone):只复制对象的第一层属性,嵌套对象仍然共享引用。
// 浅拷贝示例
const original = {
name: 'John',
address: {
city: 'Taipei',
country: 'Taiwan',
},
};
const shallowCopy = { ...original };
shallowCopy.address.city = 'Kaohsiung';
console.log(original.address.city); // 'Kaohsiung' ❌ 原始对象也被修改了
深拷贝(Deep Clone):递归复制所有层级的属性,完全独立。
// 深拷贝示例
const original = {
name: 'John',
address: {
city: 'Taipei',
country: 'Taiwan',
},
};
const deepCopy = deepClone(original);
deepCopy.address.city = 'Kaohsiung';
console.log(original.address.city); // 'Taipei' ✅ 原始对象不受影响
2. Implementation Methods
实现方法
方法 1:使用 JSON.parse 和 JSON.stringify
优点:简单快速 缺点:无法处理函数、undefined、Symbol、Date、RegExp、Map、Set 等特殊类型
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
// 测试
const original = {
name: 'John',
age: 30,
address: {
city: 'Taipei',
country: 'Taiwan',
},
hobbies: ['reading', 'coding'],
};
const cloned = deepClone(original);
cloned.address.city = 'Kaohsiung';
cloned.hobbies.push('swimming');
console.log(original.address.city); // 'Taipei' ✅
console.log(original.hobbies); // ['reading', 'coding'] ✅
限制:
const obj = {
date: new Date(),
func: function () {},
undefined: undefined,
symbol: Symbol('test'),
regex: /test/g,
};
const cloned = deepClone(obj);
console.log(cloned.date); // {} ❌ Date 变成空对象
console.log(cloned.func); // undefined ❌ 函数丢失
console.log(cloned.undefined); // undefined ✅ 但 JSON.stringify 会移除
console.log(cloned.symbol); // undefined ❌ Symbol 丢失
console.log(cloned.regex); // {} ❌ RegExp 变成空对象
方法 2:递归实现(处理基本类型和对象)
function deepClone(obj) {
// 处理 null 和基本类型
if (obj === null || typeof obj !== 'object') {
return obj;
}
// 处理 Date
if (obj instanceof Date) {
return new Date(obj.getTime());
}
// 处理 RegExp
if (obj instanceof RegExp) {
return new RegExp(obj);
}
// 处理数组
if (Array.isArray(obj)) {
return obj.map((item) => deepClone(item));
}
// 处理对象
const cloned = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
cloned[key] = deepClone(obj[key]);
}
}
return cloned;
}
// 测试
const original = {
name: 'John',
date: new Date(),
regex: /test/g,
hobbies: ['reading', 'coding'],
address: {
city: 'Taipei',
},
};
const cloned = deepClone(original);
cloned.date.setFullYear(2025);
cloned.hobbies.push('swimming');
console.log(original.date.getFullYear()); // 2024 ✅ 不受影响
console.log(original.hobbies); // ['reading', 'coding'] ✅
方法 3:完整实现(处理 Map、Set、Symbol 等)
function deepClone(obj, map = new WeakMap()) {
// 处理 null 和基本类型
if (obj === null || typeof obj !== 'object') {
return obj;
}
// 处理循环引用
if (map.has(obj)) {
return map.get(obj);
}
// 处理 Date
if (obj instanceof Date) {
return new Date(obj.getTime());
}
// 处理 RegExp
if (obj instanceof RegExp) {
return new RegExp(obj.source, obj.flags);
}
// 处理 Map
if (obj instanceof Map) {
const clonedMap = new Map();
map.set(obj, clonedMap);
obj.forEach((value, key) => {
clonedMap.set(deepClone(key, map), deepClone(value, map));
});
return clonedMap;
}
// 处理 Set
if (obj instanceof Set) {
const clonedSet = new Set();
map.set(obj, clonedSet);
obj.forEach((value) => {
clonedSet.add(deepClone(value, map));
});
return clonedSet;
}
// 处理数组
if (Array.isArray(obj)) {
const clonedArray = [];
map.set(obj, clonedArray);
obj.forEach((item) => {
clonedArray.push(deepClone(item, map));
});
return clonedArray;
}
// 处理对象
const cloned = {};
map.set(obj, cloned);
// 处理 Symbol 属性
const symbolKeys = Object.getOwnPropertySymbols(obj);
const stringKeys = Object.keys(obj);
// 复制普通属性
stringKeys.forEach((key) => {
cloned[key] = deepClone(obj[key], map);
});
// 复制 Symbol 属性
symbolKeys.forEach((symbolKey) => {
cloned[symbolKey] = deepClone(obj[symbolKey], map);
});
return cloned;
}
// 测试
const symbolKey = Symbol('test');
const original = {
name: 'John',
[symbolKey]: 'symbol value',
date: new Date(),
map: new Map([['key', 'value']]),
set: new Set([1, 2, 3]),
hobbies: ['reading', 'coding'],
};
const cloned = deepClone(original);
console.log(cloned[symbolKey]); // 'symbol value' ✅
console.log(cloned.map.get('key')); // 'value' ✅
console.log(cloned.set.has(1)); // true ✅