ES6 箭头函数是否有自己的参数?

Do ES6 arrow functions have their own arguments or not?

我不知道箭头函数是否将arguments绑定到词法作用域。

看看这个例子(同样的概念可以用于this):

var b = function() { return () => console.log(arguments); };
b(1,2,3)(4,5,6); // different result of chrome vs FF.

当我在 Chrome 上 运行 时,我得到 [1,2,3],但在 Firefox 上,我得到 [4,5,6]。怎么回事?

不,箭头函数没有自己的 argumentsthissupernew.target

请参阅 14.2.16 Runtime Semantics: Evaluation 处的注释:

An ArrowFunction does not define local bindings for arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be the Function Environment of an immediately enclosing function.

来自spec

Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment.

因此,正确答案是 [1,2,3]。 Firefox 已在版本 43 (bug 889158) 中修复该问题。

发生的事情其实很简单。 Chrome 似乎没有将 arguments 对象添加到内部(箭头)函数的范围,而 Firefox 会。

这意味着在Chrome中记录的参数是传递给父函数的参数,它是一个"normal"函数。

Firefox 相信(在我看来他们是正确的)箭头函数也应该有 arguments 对象,因此这就是他们记录第二组数字的原因。

正如其他人所说,Firefox 所做的是违反规范的。

Arrow functions don't have their own arguments object.

Arrow functions do not expose an arguments object to their code: arguments.length, arguments[0], arguments[1], and so forth do not refer to the arguments provided to the arrow function when called.

Arrow_functions

对于这个例子

var b = function() {
  return () => console.log(arguments);
};

b(1,2,3)(4,5,6);

正确答案应该是[1, 2, 3]