http://es6.ruanyifeng.com/#do...es6
含义数组
扩展运算符(spread)是三个点(...)。它比如 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。函数
console.log(...[1, 2, 3]) // 1 2 3 console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5 [...document.querySelectorAll('div')] // [<div>, <div>, <div>]
该运算符主要用于函数调用。rest
function push(array, ...items) { array.push(...items); } function add(x, y) { return x + y; } var numbers = [4, 38]; add(...numbers) // 42
上面代码中,array.push(...items)
和add(...numbers)
这两行,都是函数的调用,它们的都使用了扩展运算符。该运算符将一个数组,变为参数序列。code
扩展运算符与正常的函数参数能够结合使用,很是灵活。对象
function f(v, w, x, y, z) { } var args = [0, 1]; f(-1, ...args, 2, ...[3]);
扩展运算符后面还能够放置表达式。get
const arr = [
...(x > 0 ? ['a'] : []),
'b',
];
若是扩展运算符后面是一个空数组,则不产生任何效果。it
[...[], 1]
// [1]io