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

4

能否从外部作用域获取函数参数数量?

var foo = function(a,b,c) {
}

alert(foo.arguments.length); // how to do it? possible?

你想要期望参数的数量吗? - thefourtheye
3个回答

4
你想要获取 Function.length
function test(a, b, c) {
  // code
}

console.log(test.length); // 3

顺便提一下,函数期望的参数数量被称为arity。曾经有一个名为Function.arity的方法,但已被弃用,推荐使用Function.length


3

移除foo.arguments.length中的参数。

foo.length

3
尝试使用foo.length。它提供了函数定义中定义的参数数量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接