从外部作用域获取函数参数计数

get function arguments count from external scope

本文关键字:参数 函数 作用域 获取 从外部      更新时间:2023-09-26

是否可以从外部范围获得函数参数计数?

var foo = function(a,b,c) {
}
alert(foo.arguments.length); // how to do it? possible?

您想要Function.length:

function test(a, b, c) {
  // code
}
console.log(test.length); // 3

顺便说一句,函数的预期参数数称为。曾经有一种叫做Function.arity的方法,但现在被弃用了,取而代之的是Function.length

尝试foo.length。它没有给出函数定义中定义的任何参数

删除foo.arguments.length中的参数

foo.length