When you call a function, you can pass zero or more arguments to it from outside. These arguments are available as named variables whose name is defined in the function declaration.
However, they are also available as the elements in an array. The arguments array is referenced by the arguments property of the call object. Since the call object is added to the scope chain, you don't need to reference the arguments property with an object identifier prefix.
The array based mechanism is useful for those times when you want to implement a function that has a variable number of arguments passed to it according to how and when it is called.
A new arguments object is created for each execution context. When the flow of control enters an execution context for a function block, a new arguments object is created. Declared functions, anonymous code and implementation-specific code all use this technique.
When creating the arguments object, the initial conditions are set up like this:
The internal Prototype property for the arguments object is that returned by calling Object.Prototype.
A property is created with the name callee. The callee property cannot be enumerated. The initial value of the callee property is the function object being executed. Anonymous functions can then be executed recursively if you so desire.
A property named length is created whose value is the number of arguments passed to the function. The length property cannot be enumerated.
Each argument is associated with a property whose name is its integer position in an array of arguments. The arguments are accessed in presentation order. Although the names are strings, they represent purely numeric values and range from 0 to 1 less than the value in the length property. You can enumerate the arguments in a for loop.
Note that objects of this type can only exist within a function body in a web browser, because you cannot pass parameters to a script from outside. It is possible that an embedded JavaScript interpreter may provide a host object to the main entry point to perform the same function.
In Netscape, the arguments array is implemented as an object of type Arguments but in MSIE its type is simply an Object object. In Netscape, the arguments object is extended with a toString() mechanism that returns the arguments as a comma separated list in a String. In MSIE, you get the object type.
None of the properties of the arguments object are enumerable.
Because the arguments object is meant to be used in a manner that is local to the function it was created in, you get unpredictable results if you pass it to another function as an argument itself.
Note that at the time of writing the example given below did not seem to work on Netscape 6.0.
<SCRIPT> // Call a function and use its arguments array find out the // name of the function that called it. Demonstrates a one // level call tracer. level1(); function level1() { testArgs(1, "ONE", true); } function testArgs(a1, a2,a3) { document.write(callerName(arguments)); document.write("<BR>"); } function callerName(a1) { myCallerObject = a1.caller.callee; myCallerSource = String(myCallerObject); mySplitArray1 = myCallerSource.split(" "); mySplitArray2 = mySplitArray1[1].split("("); myCaller = mySplitArray2[0]; return(myCaller); } </SCRIPT>
ECMA 262 edition 2 - section - 10.1.6
ECMA 262 edition 2 - section - 10.1.8
ECMA 262 edition 2 - section - 15.2.3.1
ECMA 262 edition 3 - section - 10.1.6
ECMA 262 edition 3 - section - 10.1.8
Wrox Instant JavaScript - page - 27
Prev | Home | Next |
Argument list | Up | Arguments.callee |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |