手写深拷贝(JS)
代码
const oldObj = {
name: "Jujuxiaer",
age: 20,
colors: ['orange', 'red', 'blue'],
friends: {
name: "小可"
}
}
const newObj1 = oldObj;
newObj1.name = "小华";
console.log(oldObj);
console.log(newObj1);
function deepClone(obj = {}) {
let ret;
if (typeof obj !== 'object' || obj == null) {
return obj;
}
if (obj instanceof Array) {
ret = [];
} else {
ret = {};
}
for (let objKey in obj) {
// 只拷贝对象自身的属性
if (obj.hasOwnProperty(objKey)) {
// 重点:递归的深拷贝,目的是为了对象的对象的值也是深拷贝
ret[objKey] = deepClone(obj[objKey]);
}
}
return ret;
}
const oldObj2 = {
name: "Jujuxiaer",
age: 20,
colors: ['orange', 'red', 'blue'],
friends: {
name: "小可"
}
}
const newObj2 = deepClone(oldObj2);
newObj2.name = "小华";
newObj2.colors[0] = 'black';
newObj2.age = 30;
newObj2.friends.name = "小明";
console.log('=========');
console.log(oldObj2);
console.log(newObj2);
结果:
代码地址:https://github.com/Jujuxiaer/front-basic-example/blob/master/%E5%93%88%E9%BB%98%E8%81%8A%E5%89%8D%E7%AB%AF/%E6%89%8B%E5%86%99%E6%B7%B1%E6%8B%B7%E8%B4%9D/index.js