首页 > 解决方案 > Wsy 这个箭头函数不适用于“arguments.length”吗?

问题描述

我想知道为什么以下名为“countArg2”的箭头函数不起作用。有没有人可以解释什么是错的?


这有效
function countArg1() {
    return arguments.length;
}
countArg1(1, 2, 3);   //3

这个不行。。
const countArg2 = () => arguments.length;
countArg2(1, 2, 3);
 // VM6745:1 Uncaught ReferenceError: arguments is not defined

先感谢您。

标签: javascriptargumentsarrow-functions

解决方案


您必须像这样解析箭头函数的参数

const countArg2 = (...arguments) => arguments.length;
console.log(countArg2(1, 2, 3));
 // VM6745:1 Uncaught ReferenceError: arguments is not defined
 // at mArgs (<anonymous>:1:29)
 // at <anonymous>:2:1


推荐阅读