An array corresponding to the arguments passed to a function.
Function
You can call a function with more arguments than it is formally declared to accept by using the arguments array. This technique is useful if a function can be passed a variable number of arguments. You can use arguments.length to determine the number of arguments passed to the function, and then treat each argument by using the arguments array.
The arguments array is available only within a function declaration. Attempting to access the arguments array outside a function declaration results in an error.
The this keyword does not refer to the currently executing function, so you must refer to functions and Function objects by name, even within the function body.
In JavaScript 1.2, arguments includes these additional properties:
formal arguments—Each formal argument of a function is a property of the arguments array.
local variables—Each local variable of a function is a property of the arguments array.
caller—A property whose value is the arguments array of the outer function. If there is no outer function, the value is undefined.
For example, the following script demonstrates several of the arguments properties:
function b(z) { Console.Write(arguments.z) Console.Write (arguments.caller.x) return 99 } function a(x, y) { return b(534) } Console.Write (a(2,3)) This displays: 534 2 99
534 is the actual parameter to b, so it is the value of arguments.z. 2 is a's actual x parameter, so (viewed within b) it is the value of arguments.caller.x. 99 is what a(2,3) returns.
This example defines a function that creates test lists. The only formal argument for the function is a string that changes the appearance of the list. To create a bullet list (also called an "unordered list"), use "U". To create a numbered list (also called an "ordered list"), use "O". The function is defined as follows:
function list(type) { Console.Write(type) for (var i=1; i<list.arguments.length; i++) { Console.Write(list.arguments[i]) Console.Write(type) } }
You can pass any number of arguments to this function, and it displays each argument as an item in the type of list indicated. For example, the following call to the function: