Friday, October 23, 2009

Section 24.5. Arguments.length: the number of arguments passed to a function










24.5. Arguments.length: the number of arguments passed to a function


ECMAScript v1



24.5.1. Synopsis



arguments.length





24.5.2. Description


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.




24.5.3. Example



// Use an Arguments object to check that correct # of args were passed
function check(args) {
var actual = args.length; // The actual number of arguments
var expected = args.callee.length; // The expected number of arguments
if (actual != expected) { // Throw exception if they don't match
throw new Error("Wrong number of arguments: expected: " +
expected + "; actually passed " + actual);
}
}
// A function that demonstrates how to use the function above
function f(x, y, z) {
check(arguments); // Check for correct number of arguments
return x + y + z; // Now do the rest of the function normally
}





24.5.4. See Also



Array.length, Function.length














No comments:

Post a Comment