JavaScript Arguments Object



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site




Introduction And Review

JavaScript has two special objects that help with function processing. These are the:

These two objects work together and have some common characteristics:





Presentation Conventions for this Page





The Arguments Object Description

The Arguments Object is a special temporary object that exists while a function is active. The function body has access to the Arguments Object and to a special property of the Call Object called the "arguments property". The arguments property points to the Arguments Object and is used to access the properties of the Arguments Object. The properties of the Arguments Object are summarized here with the code to access the property:

numbered properties (the elements)
arguments[0]
length property
arguments.length
callee property
arguments.callee

Given that your function's formal parameters are named, the programmer has two options from within the function body of referencing the parameters that are passed to the function: 1) by name, 2) by the arguments property

The Arguments Object has some interesting characteristics:





The Elements of the Arguments Object

As noted above, the Arguments Object behaves like an array but is not technically an array. The Arguments Object contains numbered properties that represent the arguments passed to the function at the time it was invoked. These numbered properties are accessed via the arguments[ ] property. For example the following line of code inside the function body: alert(arguments[0]); will access the first argument passed to the function in the function parameter list.





The arguments.callee Property

The arguments.callee property refers to the function that is currently active. This property provides a way for an unnamed function to refer to itself. Recall that literal functions are normally unnamed and this combination allows for recursion. The arguments.callee property is only available from within the function body. Below we show two approaches to recursive processing. The later approach uses the arguments.callee property.

A recursive function for the factorial problem using a named function:

//recursive function using a named function var n = 0; function factorial(n) { // the following selection control structure controls the recursion  if (n == 0)    return 1; // the base case  else    return n * factorial(n-1); // the recursive case } alert(factorial(4)); // pass four to the recursive function

A recursive function for the factorial problem using a unnamed function literal:

//recursive function using the callee property to refer to itself var n = 0; var factorial = function(n) { // the following selection control structure controls the recursion  if (n == 0)    return 1; // the base case  else    return n * arguments.callee(n-1); // the recursive case } alert(factorial(4)); // pass four to the recursive function



The arguments.length Property

The arguments.length property provides the number of arguments passed to the current function. Like the other properties of the Arguments Object, the arguments.length property is only available from within the function body.

The arguments.length property can be used to validate that the actual parameters passed to the function matches the formal parameters expected. Remember, JavaScript does not perform this aspect of the signature validation.



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson