Official information on `arguments` in ES6 Arrow functions?

白昼怎懂夜的黑 提交于 2019-11-25 19:46:04
Bergi

Chrome, FF, and node seem to be wrong here, Babel is correct:

Arrow functions do not have an own arguments binding in their scope; no arguments object is created when calling them.

looking for official docs here

Arrow function expressions evaluate to functions that have their [[ThisMode]] set to lexical, and when such are called the declaration instantiation does not create an arguments object. There is even a specifc note (18 a) stating that "Arrow functions never have an arguments objects.".

As noted by Bergi, arrow functions do not have their own arguments variable.

However, if you do want to capture the args for your arrow function, you can simply use a rest parameter

const myFunc = (...args) =>
  console.log ("arguments", args)
  
myFunc (1, 2, 3)
// arguments [1, 2, 3]

Rest parameters can be combined with other positional parameters, but must always be included as the last parameter

const myFunc = (a, b, c, ...rest) =>
  console.log (a, b, c, rest)

myFunc (1, 2, 3, 4, 5, 6, 7)
// 1 2 3 [ 4, 5, 6, 7 ]

If you make the mistake of writing a rest parameter in any other position, you will get an Error

const myFunc = (...rest, a, b, c) =>
  console.log (a, b, c, rest)
  
myFunc (1, 2, 3, 4, 5, 6, 7)
// Error: Rest parameter must be last formal parameter
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!