Arguments.caller (Property)

The object that called the function that owns the arguments.

Availability:

JavaScript - 1.1
JScript - 5.5
Internet Explorer - 5.5
Netscape - 3.0
Deprecated
Property/method value type:Arguments.object
JavaScript syntax:-myArguments.caller

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

Warnings:

Example code:

   <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>

See also:Arguments object, Arguments.callee, Debugging - client side, Function object, Function.caller, Hierarchy of objects

Property attributes:

DontEnum.