对象的赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| let a = {'name': '小王', 'age': 12};
let b = a;
b.sex = 'boy';
b.name = "王五";
console.log(a, b) console.log(b === a);
|
对b的修改会影响a原本的值。对a的修改同样会同步b的值.
对象深拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
export const copyObject = (obj) => { if (!(obj instanceof Array)) { let newObj = Object.assign({}, obj); return newObj; } else { return obj.map((item) => { let newObj = Object.assign({}, item); if (item.children && item.children.length > 0) { let temp = copyObject(item.children); newObj.children = temp; } return newObj; }); } }
|