对象深拷贝实现
对象深拷贝实现
//递归实现
const deepClone = (obj) => {
let newObj = {};
for (const i in obj) {
if (Object.hasOwnProperty.call(obj, i)) {
if (obj[i] instanceof Object) {
newObj[i] = deepClone(obj[i]);
} else {
newObj[i] = obj[i];
}
}
}
return newObj;
}
//序列化实现
const deepCloneWithJSON=(obj)=>{
const str=JSON.stringify(obj);
return JSON.parse(str);
}