小编Sha*_*tri的帖子

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

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

箭头功能:

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)

javascript arrow-functions

5
推荐指数
1
解决办法
434
查看次数

标签 统计

arrow-functions ×1

javascript ×1