Why do arrow functions not have the arguments array? [duplicate]

纵然是瞬间 提交于 2019-12-17 04:01:31

问题


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);


来源:https://stackoverflow.com/questions/41731854/why-do-arrow-functions-not-have-the-arguments-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!