改变obj2的k2和k3属性,为什么obj1完全不受影响?
function shallowClone(obj){
let newObj = {};
for(const key in obj){
newObj[key] = obj[key];
}
return newObj;
}
let obj1 = {
k1:123,
k2:{name:'k2'},
k3:[1,2,[3,4]]
};
let obj2 = shallowClone(obj1);
obj2.k2 = {};
obj2.k3 = [1,2,3,4];
console.log(obj1);
console.log(obj2);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…