1、将一个数组变为一个以空格分隔的参数序列
const arr=[1,2,3,4,5]
console.log(...arr);//1 2 3 4 5
2、复制
如果存储的数据为基本数据类型,则为深拷贝;如果存放的是引用数据类型,就是浅拷贝;
复制数组
const arr1=[1,2,3,4,[5]];
const arr2=[...arr1];
arr1[0]=10;
arr1[4][0]=100;
console.log(arr1);//[ 10, 2, 3, 4, [ 100 ] ]
console.log(arr2);//[ 1, 2, 3, 4, [ 100 ] ]
复制对象
const obj1={name:"Tom",age:18,person:{name:"Bob",age:28}};
const obj2={...obj1};
obj1.age=100;
obj1.person.name="王麻子";
console.log(obj1);//{ name: 'Tom', age: 100, person: { name: '王麻子', age: 28 } }
console.log(obj2);//{ name: 'Tom', age: 18, person: { name: '王麻子', age: 28 } }
3、合并
//合并数组
const arr1=[1,2,3];
const arr2=[4,5,6];
const arr3=[...arr1,...arr2];
console.log(arr3);//[ 1, 2, 3, 4, 5, 6 ]
//合并对象
const obj1={name:"张三"};
const obj2={age:18};
const obj3={...obj1,...obj2};
console.log(obj3);//{ name: '张三', age: 18 }
4、字符串改为数组
const myArr=[..."hello"];
console.log(myArr);//[ 'h', 'e', 'l', 'l', 'o' ]