Why do arrow functions not have the arguments arra

2019-01-08 22:03发布

问题:

This question already has an answer here:

  • Official information on `arguments` in ES6 Arrow functions? 2 answers
function foo(x) {
   console.log(arguments)
} //foo(1) prints [1]

but

var bar = x => console.log(arguments) 

gives the following error when invoked in the same way:

Uncaught ReferenceError: arguments is not defined

回答1:

Arrow functions don't have this since the arguments array-like object was a workaround to begin with, which ES6 has solved with a rest parameter:

var bar = (...arguments) => console.log(arguments);

arguments is by no means reserved here but just chosen. You can call it whatever you'd like and it can be combined with normal parameters:

var test = (one, two, ...rest) => [one, two, rest];

You can even go the other way, illustrated by this fancy apply:

var fapply = (fun, args) => fun(...args);