深拷贝

手写深拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 深拷贝
* @param {Object} obj 要拷贝的对象
* @param {Map} map 用于存储循环引用对象的地址
*/

function deepClone(obj = {}, map = new Map()) {
if (typeof obj !== "object") {
return obj;
}

if (map.get(obj)) {
return map.get(obj);
}

let result = {};
// 加 || 的原因是为了防止 Array 的 prototype 被重写,Array.isArray 也是如此
if (obj instanceof Array || Object.prototype.toString === "[object Array]") {
result = [];
}
// 防止循环引用
map.set(obj, result);
for (const key in obj) {
// 保证 key 不是原型属性
if (obj.hasOwnProperty(key)) {
// 递归调用
result[key] = deepClone(obj[key], map);
}
}
return result;
}