vue3+es6语法
直接上代码
javascript
const objA = { name: '小飞', age: 18 };
const objACopy = { ...objA };
console.log('对比objA与objACopy的引用地址是否相同');
console.log(objA == objACopy); //false
console.log(objA);
console.log(objACopy);
//对象包含对象,浅拷贝
const objB = { name: '小飞', age: 18, hobbies: ['football', 'badminton'] };
const objBCopy = { ...objB };
console.log('对比objB与objBCopy的引用地址是否相同');
console.log(objB.hobbies == objBCopy.hobbies); //true
console.log(objB);
console.log(objBCopy);
//对象包含对象 改造一下,深拷贝
const objBCopyDeep = { ...objB, hobbies: { ...objB.hobbies } };
console.log(objB.hobbies == objBCopyDeep.hobbies); //false