浅拷贝创建一个新对象,这个对象仅对原对象的属性进行拷贝,属性值是基本类型时,拷贝的是原数据,属性值是引用类型时,拷贝的是指针
let person = { name: 'swt', age: 20 }
let people = Object.assign({}, person)
people.name = 'pty'
console.log(person) // { name: 'swt', age: 20 }
console.log(people) // { name: 'pty', age: 20 }
let arr = [1, 2, 3]
let newArr = arr.concat()
newArr[0] = 100
console.log(arr) // [ 1, 2, 3 ]
console.log(newArr) // [ 100, 2, 3 ]
const animals = ["ant", "bison", "camel", "duck", "elephant"]
const newAnimals = animals.slice();
newAnimals[0] = "chicken"
console.log(animals); // [ 'ant', 'bison', 'camel', 'duck', 'elephant' ]
console.log(newAnimals); // [ 'chicken', 'bison', 'camel', 'duck', 'elephant' ]
const a = [1, 2, 3]
const b = [...a]
b[0] = 4
console.log(a); // [ 1, 2, 3 ]
console.log(b); // [ 4, 2, 3 ]