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

Sha*_*tri 5 javascript arrow-functions

我想了解正常函数与箭头函数的行为.

箭头功能:

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


console.log(arrowFunc(1, 2, 3)(1))
Run Code Online (Sandbox Code Playgroud)

正常功能

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

console.log(normalFunc(1, 2, 3)(1))
Run Code Online (Sandbox Code Playgroud)

两个结果都是相同的,但看起来像上面定义的arrowFunc考虑第一个arg列表,其中normalFunc考虑第二组arg列表.

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

通天输出:

"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));
Run Code Online (Sandbox Code Playgroud)

Rob*_*sen 10

预计结果都是一样的

不,他们不是.


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

箭头函数表达式比函数表达式更短的语法,没有自己的this,arguments,super,或 new.target.

在同一页面上:

箭头函数没有自己的arguments对象.因此,在这个例子中,arguments只是对封闭范围的参数的引用 [...]

ECMAScript规范中:

注意:箭头函数永远不会有参数对象.(原文如此)