深拷贝
ts
function deepClone<T>(value: T): T {
const cache = new WeakMap();
function _deepClone(obj: any): any {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
if (cache.has(obj)) {
return cache.get(obj);
}
const result = Array.isArray(obj) ? [] : ({} as any);
cache.set(obj, result);
for (const key in obj) {
result[key] = _deepClone(obj[key]);
}
return result;
}
return _deepClone(value);
}