应用场景: 赋值运算中 浅拷贝:指向的是堆内存中的同一个对象 深拷贝:指向堆内存中的不同的对象
1、浅拷贝
简单的赋值运算—浅拷贝
var arr = [1,2,3,4]
var newArr = arr
newArr.push(5)
console.log(arr);//1,2,3,4,5
2、深拷贝
赋值运算的时候只赋值,不赋址
var arr1 = [1,2,3,4]
var newArr1 = [...arr1]
arr1.push(5)
console.log(newArr1);//1,2,3,4
var arr2 = [2,3,4,5]
var newArr2 = arr2.slice(0,)
arr2.push(6)
console.log(newArr2);//2,3,4,5
var newArr1 = [].concat(arr1)
arr1.push(5)
console.log(newArr1)
var arr1 = [{name:'lisi'},{name:"wangwu"}]
/* var newArr1 = [...arr1]
arr1[0].name = 'react'
console.log(newArr1); *///name:react
var newArr1 = JSON.parse(JSON.stringify(arr1))
arr1[0].name = 'react'
console.log(newArr1);//name:"lisi"