说明
扩展运算符(spread)是三个点(…), 将一个数组转为用逗号分隔的参数序列 。
简单用法
//基础用法 console.log(...[1, 2, 3]); //输出 :1 2 3 console.log(1, ...[2, 3, 4], 5); //输出 :1 2 3 4 5 //进阶用法1(函数传参) let print = (x, y) => { return x + y }; let number = [1, 2]; console.log(print(...number)); //输出: 3 //进阶用法2(数组合并) let arr = [...[1, 2, 3], ...[4, 5, 6]]; console.log(arr); //输出[1,2,3,4,5,6] //进阶用法3(与解构表达式结合) const [first, ...rest] = [1, 2, 3, 4]; console.log(first, rest); //输出: 1 [2,3,4] //进阶用法4(将字符串转成数组) console.log([..."hello"]) //输出: ["h","e","l","l","o"]