May 2011
Intermediate to advanced
1093 pages
40h 54m
English
Arguments.length — the number of arguments passed to a function
arguments.length
The length property of the
Arguments object specifies the number of arguments passed to the
current function. This property is defined only within a function
body.
Note that this property specifies the number of arguments
actually passed, not the number expected. See Function.length for the number of declared
arguments. Note also that this property does not have any of the
special behavior of the Array.length property.
// Use an Arguments object to check that correct # of args were passedfunctioncheck(args){varactual=args.length;// The actual number of argumentsvarexpected=args.callee.length;// The expected number of argumentsif(actual!=expected){// Throw exception if they don't matchthrownewError("Wrong number of arguments: expected: "+expected+"; actually passed "+actual);}}// A function that demonstrates how to use the function abovefunctionf(x,y,z){check(arguments);// Check for correct number of argumentsreturnx+y+z;// Now do the rest of the function normally}