File tree Expand file tree Collapse file tree 1 file changed +4
-12
lines changed Expand file tree Collapse file tree 1 file changed +4
-12
lines changed Original file line number Diff line number Diff line change @@ -16,15 +16,7 @@ arr.concat();
16
16
17
17
### 深拷贝
18
18
19
- 我们也能用简单的办法实现深拷贝:
20
-
21
- ` JSON.parse(JSON.stringify(obj)) `
22
-
23
-
24
-
25
- 手动实现:
26
-
27
- > 代码解析后面放出来。
19
+ 深拷贝解析:[ 如何写出一个惊艳面试官的深拷贝.] ( http://www.conardli.top/blog/article/JS%E8%BF%9B%E9%98%B6/%E5%A6%82%E4%BD%95%E5%86%99%E5%87%BA%E4%B8%80%E4%B8%AA%E6%83%8A%E8%89%B3%E9%9D%A2%E8%AF%95%E5%AE%98%E7%9A%84%E6%B7%B1%E6%8B%B7%E8%B4%9D.html )
28
20
29
21
``` js
30
22
const mapTag = ' [object Map]' ;
@@ -139,22 +131,22 @@ function clone(target, map = new WeakMap()) {
139
131
140
132
// 防止循环引用
141
133
if (map .get (target)) {
142
- return target;
134
+ return map . get ( target) ;
143
135
}
144
136
map .set (target, cloneTarget);
145
137
146
138
// 克隆set
147
139
if (type === setTag) {
148
140
target .forEach (value => {
149
- cloneTarget .add (clone (value));
141
+ cloneTarget .add (clone (value,map ));
150
142
});
151
143
return cloneTarget;
152
144
}
153
145
154
146
// 克隆map
155
147
if (type === mapTag) {
156
148
target .forEach ((value , key ) => {
157
- cloneTarget .set (key, clone (value));
149
+ cloneTarget .set (key, clone (value,map ));
158
150
});
159
151
return cloneTarget;
160
152
}
You can’t perform that action at this time.
0 commit comments