如果我的函数需要变量,是否可以知道?
例如:
function ada (v) {};
function dad () {};
alert(ada.hasArguments()); // true
alert(dad.hasArguments()); // false
答案 0 :(得分:17)
是。函数的length
属性返回声明的参数数量:
alert(ada.length); // 1
alert(dad.length); // 0
答案 1 :(得分:4)
函数的length
属性表示形式参数的数量。请注意,这不一定等于实际参数的数量:
function foo(one, two, three) {
return foo.length === arguments.length;
}
foo("test");
foo("test", "test", "test");
输出:
false
true