箭头函数与 Javascript 中函数的行为差异

Behaviour Difference For Arrow-Functions vs Functions In Javascript

我想了解普通函数与箭头函数的行为。

箭头函数:

function arrowFunc() {
  return () => arguments
}


console.log(arrowFunc(1, 2, 3)(1))

正常功能

function normalFunc() {
  return function() {
    return arguments
  }
}

console.log(normalFunc(1, 2, 3)(1))

这两个结果预计是相同的,但看起来上面定义的 arrowFunc 考虑第一个 arg 列表,而 normalFunc 考虑第二组 arg 列表。

也尝试了 babel-compilation 来了解差异,但看起来行为有所不同,如下所示:

Babel 输出:

"use strict";

function arrowFunc() {
  var _arguments = arguments;

  return function() {
    return _arguments;
  };
}

console.log(arrowFunc(1, 2, 3)(1));

function normalFunc() {
  return function() {
    return arguments;
  };
}

console.log(normalFunc(1, 2, 3)(1));

Both the results are expected to be same

不,他们不是。


来自 MDN 箭头函数页面的第一行(强调我的):

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target.

在同一页的下方:

Arrow functions do not have their own arguments object. Thus, in this example, arguments is simply a reference to the arguments of the enclosing scope [...]

并且在 ECMAScript specification:

NOTE: Arrow functions never have an arguments objects. (sic)