This property refers to an Arguments object belonging to a parent function. Function call tracing can traverse a hierarchy based on Arguments objects to unwind a call stack. This might be useful when debugging complex script projects.
You can work out the calling tree by tracing the callee and caller relationships back up the execution context tree. The caller is a reference to the arguments object of the caller of the function.
To reference the function that called the current one, use this:
arguments.caller.callee
To get the name of the function that called the current one use this (so long as the interpreter supports the name property on functions):
arguments.caller.callee.name
With this, you could build a stack trace function that you can call and will unwind the calling context stack to show you how you got to the location you are in. Tools such as this are useful to have around and if they are in a separate .js file, you can include them when you need to debug a script problem.
When the caller value is null, it refers to the global code context because there is no arguments array in that context - at least not in a web browser. Other host implementations may provide an additional level of arguments according to how the script is executed.
This has no meaning outside of the context of a function.
The example shows how to walk up the calling tree and should yield the following output when it is run:
level2
called by level1
called by global level
This property is incorrectly implemented in Netscape 3, which returned a reference to the calling function and not its arguments. Since it works correctly in Netscape 4, you should consider that it is only available there.
The example shown below did not work correctly on Netscape 6.0 at time of writing.
This is not part of the ECMA standard and is at some risk of becoming deprecated and removed in later versions. In fact, it is deprecated as of JavaScript version 1.3 and should not be used in new projects.
It is recommended that you do not build this into functional deployed applications, although the risks involved with using it for debugging are small.
<SCRIPT> // A function to extract the calling function name when // passed the arguments object from a function. Demonstrates // how to recursively walk up a call tree. level1(); function level1() { level2(); } function level2() { testArgs(1, "ONE", true); } function testArgs(a1, a2,a3) { document.write(callerName(arguments)); document.write("<BR>"); } function callerName(a1) { if(a1.caller == null) { return("global level"); } myCallerObject = a1.caller.callee; myCallerSource = String(myCallerObject); mySplitArray1 = myCallerSource.split(" "); mySplitArray2 = mySplitArray1[1].split("("); myCaller = mySplitArray2[0]; return(myCaller+"<BR> called by "+callerName(a1.caller)); } </SCRIPT>
Prev | Home | Next |
Arguments.callee | Up | Arguments.length |
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. |