1.JSON对象序列化方法
这个方法明显是简单得多,但是有个弊端,就是不能复制函数
var obj = {a:1,b:2} var newObj = JSON.parse(JSON.stringify(obj));
obj.c = 3;
console.log(obj,newObj);
2.dom元素的复制——cloneNode
<div id="box"></div>
let div = document.getElementById('box'); let box2 = div.cloneNode(true);
console.log(div,box2);
3.es6新方法——Object.assign
//比较常用
var obj = {a:1,b:2} var newObj = Object.assign({}, obj);
obj.c = 3;
console.log(obj,newObj);
4.es6新方法——扩展运算符(...)
var obj = {a:1,b:2} var newObj ={...obj}
obj.c = 3;
console.log(obj,newObj);