vue对象复制(使用es6对象扩展运算符,深拷贝)

vue3+es6语法
直接上代码

   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

vue对象复制(使用es6对象扩展运算符,深拷贝)_第1张图片

你可能感兴趣的:(vue.js,es6,vue.js,es6)