【问题标题】:confused with arguments.length in a named arrow function [duplicate]与命名箭头函数中的 arguments.length 混淆[重复]
【发布时间】:2017-11-13 16:06:23
【问题描述】:

我试图使用 arguments.length 来获取 命名函数的长度:

var a = function(b,c){
console.log(arguments.length);
};
a(1,2); //2 (this is what I'm expecting)


(function(b,c){
console.log(arguments.length);
})(1,2); //it returns 2 also


(b,c) => {
console.log(arguments.length);
};
(1,2); //2 also

但是当我尝试使用命名箭头函数时:

let a = (b,c) => {
console.log(arguments.length);
};
a(1,2); //ReferenceError: arguments is not defined

还有这个:

((b,c) => {
console.log(arguments.length);
})(1,2); //ReferenceError: arguments is not defined

我现在真的很困惑

【问题讨论】:

    标签: javascript ecmascript-6 arguments arrow-functions


    【解决方案1】:

    Arrow functions do not bind the arguments magic variable.你的第一次测试完全错误;表达式(1,2) 的返回值为2,但这只是因为2, 运算符的最后一个操作数,而不是因为arguments.length。在该示例中甚至没有调用“匿名箭头函数”。

    【讨论】:

      猜你喜欢
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 2018-02-07
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2019-01-17
      相关资源
      最近更新 更多