浅拷贝
let obj = {a:1,b:2,c:{d:1}}
let cloneObj = {...obj}
or
let obj = {a:1,b:2,c:{d:1}}
let cloneObj = Object.assign({},obj)
深拷贝
let obj = {a:1,b:2,c:{d:1}}
let cloneObj = JSON.parse(JSON.stringify(obj))
or
function cloneObject(obj) {
var clone;
if (obj instanceof Object) {
if (!(obj instanceof Function)) {
clone = obj instanceof Array ? new Array() : new Object();
for (var k in obj) {
clone[k] = cloneObj(obj[k]);
}
} else {
clone = obj
}
} else {
clone = obj
}
return clone;
}
let obj = {a:1,b:2,c:{d:1}}
let cloneObj = cloneObject(obj)