为什么箭头函数没有参数数组?
Why do arrow functions not have the arguments array?
function foo(x) {
console.log(arguments)
} //foo(1) prints [1]
但是
var bar = x => console.log(arguments)
以相同方式调用时出现以下错误:
Uncaught ReferenceError: arguments is not defined
箭头函数没有这个,因为 arguments
array-like 对象是一个解决方法,ES6 已经用 rest
参数解决了这个问题:
var bar = (...arguments) => console.log(arguments);
arguments
在这里绝不是保留而是选择。你可以随意调用它,它可以与普通参数结合使用:
var test = (one, two, ...rest) => [one, two, rest];
你甚至可以走另一条路,用这个花哨的应用来说明:
var fapply = (fun, args) => fun(...args);
function foo(x) {
console.log(arguments)
} //foo(1) prints [1]
但是
var bar = x => console.log(arguments)
以相同方式调用时出现以下错误:
Uncaught ReferenceError: arguments is not defined
箭头函数没有这个,因为 arguments
array-like 对象是一个解决方法,ES6 已经用 rest
参数解决了这个问题:
var bar = (...arguments) => console.log(arguments);
arguments
在这里绝不是保留而是选择。你可以随意调用它,它可以与普通参数结合使用:
var test = (one, two, ...rest) => [one, two, rest];
你甚至可以走另一条路,用这个花哨的应用来说明:
var fapply = (fun, args) => fun(...args);