1. 函数调用,替代apply()方法
ES5使用apply方法来可将一个数组展开成多个参数
function fn(a, b, c) { }
const args = [0, 1, 2];
fn.apply(null, args);
ES6使用…(展开运算符)
function fn(a, b, c) { }
const args = [0, 1, 2];
fn(...args);
2. 复制数组
const a1 = [0, 1, 2];
const a2 = [...a1];
console.log(a2); //[0, 1, 2]
3. 合并数组
const arr1 = ['a', 'b', 'c'];
const arr2 = [...arr1, 'd'];
cosole.log(arr2); //['a', 'b', 'c', 'd']
4. 与解构赋值结合
const [first, ...rest] = [1, 2, 3, 4, 5];
console.log(first); // 1
console.log(rest); // [2, 3, 4, 5];
//将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错
5. 将arguments或者NodeList转换为Array
const nodeList = document.querySelectorAll('div');
const array = [...nodeList];
6. 将字符串转成数组
const str = [...'hello'];
console.log(str ); // ["h", "e", "l", "l", "o"]
7. 使用Math函数
const numbers = [9, 3, 6, 2, 7];
Math.max(...numbers); // 9
Math.min(...numbers); // 2