myFunction.arguments in ECMAScript 262
An array of all parameters passed to the function/method.
- Property Type
- Array
The arguments
array is not actually a property of an instance of a Function
object, but rather is created and made available to the function when it is called. This array is useful for determining how many of a function's declared parameters have been passed, or for accessing extra parameters which were not declared, but which were passed.
function AddThreeOrMore(){
if (arguments.length<3) return null; // You must pass at least three numbers!
var total=0;
for (var i=0,len=arguments.length;i<len;i++) total += arguments[i]*1;
return total;
}
var val = AddThreeOrMore(1,1);
// ** val is null, because not enough parameters were passed to the function
var val = AddThreeOrMore(1,1,1,1,1,1,1,1,1);
// ** val is 9
The arguments
array also has a custom property named callee
, which is a pointer to the current function being executed. This property allows a function to call itself without having to know its name or context:
function Factorial(n){
if (Math.round(n)!=n || n<=0) return 0;
if (n==1) return 1;
return n * arguments.callee( n-1 );
}
var sixFactorial = Factorial(6);
// ** sixFactorial is 720