1,数组深copy
vara1=[["1","2","3"],"2","3"],a2;
a2=a1.slice(0);
a2则为a1的深copy模式(当主动修改a1时,a2就不会相对改变)
2,对象深copy
varobj1={a:0,b:{c:0}};
varobj2=JSON.parse(JSON.stringify(obj1));//先将obj1转换为JSON字符串,然后再转回对象
1,数组深copy
vara1=[["1","2","3"],"2","3"],a2;
a2=a1.slice(0);
a2则为a1的深copy模式(当主动修改a1时,a2就不会相对改变)
2,对象深copy
varobj1={a:0,b:{c:0}};
varobj2=JSON.parse(JSON.stringify(obj1));//先将obj1转换为JSON字符串,然后再转回对象
本文标题:数组和对象的深拷贝
本文链接:https://www.haomeiwen.com/subject/grstvctx.html
网友评论