egoist / you-dont-know-es6

《You don't know front-end》series - ECMAScript 2015
12 stars 0 forks source link

展开操作符和函数参数 #3

Open egoist opened 8 years ago

egoist commented 8 years ago

在 ES5 中把函数参数转换为数组:

function foo() {
   console.log([].slice.call(arguments))
}
foo(1, 2, 3)
// yield [1, 2, 3]

在 ES6 中:

function foo(...args) {
   console.log(args)
}
foo(1, 2, 3)
// yield [1, 2, 3]

反之,把数组转换为函数参数来使用:

Math.max(...[13, 7, 30])
// equals to
Math.max(13, 7, 30)

参考资料:

micooz commented 8 years ago

How about ...{}?