1.数组深拷贝
var array = [{name: ‘jack‘}, 2, 3]
var newArr = array
var copyArr = [...array]
array.push({
age: 18
})
console.log(array, ‘array‘) //changed
console.log(newArr, ‘newArr‘) // changed
console.log(copyArr, ‘copyArr‘) // keep origin array
2.对象深拷贝
var obj = {
name: ‘gaby‘
}
var newObj = obj
var copyObj = {...obj}
obj.gender = ‘female‘
console.log(obj, ‘obj‘) //changed
console.log(newObj, ‘newObj‘) // changed
console.log(copyObj, ‘copyObj‘) //keep origin obj