简体   繁体   English

关于 ES6 箭头函数中`arguments` 的官方信息?

[英]Official information on `arguments` in ES6 Arrow functions?

(() => console.log(arguments))(1,2,3);

// Chrome, FF, Node give "1,2,3"
// Babel gives "arguments is not defined" from parent scope

According to Babel (and from what I can tell initial TC39 recommendations), that is "invalid" as arrow functions should be using their parent scope for arguments.根据 Babel(以及我可以告诉最初的 TC39 建议),这是“无效的”,因为箭头函数应该使用它们的父作用域作为参数。 The only info I've been able to find that contradicts this is a single comment saying this was rejected by TC39, but I can't find anything to back this up.我能找到的唯一与此相矛盾的信息是一条评论说这被 TC39 拒绝了,但我找不到任何支持这一点的信息。

Just looking for official docs here.只是在这里寻找官方文档。

As noted by Bergi, arrow functions do not have their own arguments variable.正如 Bergi 所指出的,箭头函数没有自己的arguments变量。

However, if you do want to capture the args for your arrow function, you can simply use a rest parameter但是,如果您确实想为箭头函数捕获参数,则可以简单地使用rest 参数

 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如果你在任何其他位置写了一个rest参数的错误,你会得到一个错误

 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

Chrome, FF, and node seem to be wrong here, Babel is correct: Chrome、FF 和 node 在这里似乎是错误的,Babel 是正确的:

Arrow functions do not have an own arguments binding in their scope;箭头函数在其作用域内没有自己的arguments绑定; 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 .箭头函数表达式计算其 [[ThisMode]] 设置lexical函数,并且在调用时声明实例化不会创建arguments对象 There is even a specifc note (18 a) stating that " Arrow functions never have an arguments objects. ".甚至有一个特定的注释 (18 a) 指出“箭头函数永远不会有参数对象。 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM