In Javascript, how can one determine the number of formal parameters defined for a function?
Note, this is not the arguments
parameter when the function is called, but the number of named arguments the function was defined with.
function zero() {
// Should return 0
}
function one(x) {
// Should return 1
}
function two(x, y) {
// Should return 2
}
> zero.length
0
> one.length
1
> two.length
2
A function can determine its own arity (length) like this:
// For IE, and ES5 strict mode (named function)
function foo(x, y, z) {
return foo.length; // Will return 3
}
// Otherwise
function bar(x, y) {
return arguments.callee.length; // Will return 2
}