我试了... 这种方法 深拷贝 是不是说就能替代 深拷贝函数deepClone了
来源:7-3 对象扩展:Rest & Spread
![](http://img1.sycdn.imooc.com/user/5964526700019aa601000100-100-100.jpg)
微微一笑很闪人
2021-12-07
这种方法和deepClone 这种深拷贝封装的方法 有什么区别吗
let obj1 = {
a: {
c: /a/,
d: undefined,
b: null,
},
b: function () {
console.log(this.a);
},
c: [
{
a: "c",
b: /b/,
c: undefined,
},
"a",
3,
],
};
let obj2 = {
...obj1
}
obj3 = JSON.parse(JSON.stringify(obj1))
console.log(obj2);
// obj2.b()
console.log(obj3)
写回答
2回答
-
喵咪老师
2021-12-09
obj3 = JSON.parse(JSON.stringify(obj1))
这种深拷贝 拷贝 Date类型 就会转为字符串了。
所以不大好
10 -
谢成
2021-12-08
1、扩展运算符属于浅拷贝
2、JSON.parse(JSON.stringfy(xxx)) 属于深拷贝,但对于一些特殊情况也会被忽略,比如function、symbol
112021-12-15
相似问题