Skip to content

Commit 2c6a931

Browse files
committed
feat: deepCopy multi solve ways
1 parent 84e1690 commit 2c6a931

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

JS/algorithm/deepCopy.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
const obj = {
2+
a1: () => {
3+
console.log()
4+
},
5+
sunObj: {
6+
a2: 2
7+
},
8+
a3: new Map()
9+
}
10+
11+
export function deepCopy2(from) {
12+
return JSON.parse(JSON.stringify(from))
13+
}
14+
console.log(deepCopy2(obj))
15+
console.log(deepCopy(obj))
16+
117
// 用记忆化解决循环引用问题:如果某个对象已经拷贝过了,就直接返回之前的拷贝
218
// 映射: 原对象 -> 拷贝对象
319
function deepCopy(from, memo = new WeakMap()) {

0 commit comments

Comments
 (0)