caller

Returns the name of the function that invoked the currently executing function.

Property of

Function

Description

In JavaScript 1.4, the caller property is available only within the body of a function. If used outside a function declaration, the caller property is null.

If the currently executing function was invoked by the top level of a JavaScript program, the value of caller is null.

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.

The caller property is a reference to the calling function, so if you use it in a string context, you get the result of calling functionName.toString. That is, the decompiled canonical source form of the function.

You can also call the calling function, if you know what arguments it might want. Thus, a called function can call its caller without knowing the name of the particular caller, provided it knows that all of its callers have the same form and fit, and that they will not call the called function again unconditionally (which would result in infinite recursion).

In JavaScript 1.4 arguments.caller was an intrinsic object available to any function. Using Arguments you could examine such properties as the parameters you were called with and who called you. For example, you might have added:

function myFunc() {
   if (arguments.caller == null) {
      return ("The function was called from the top!");
   } else
      return ("This function's caller was " + arguments.caller.callee.toString().split("(")[0].replace("function ",""))
}

In JavaScript 1.5 this has been changed implementing of a direct function property that is more directly accessible. Now you would need to replace the above script by adding:

function myFunc() {
   if (myFunc.caller == null) {
      return ("The function was called from the top!");
   } else
      return ("This function's caller was " + myFunc.caller.toString().split("(")[0].replace("function ",""))
}

Calling the script has not changed in JavaScript 1.5:

Console.Writeln(myFunc())
function localFunction(){
   Console.Writeln(myFunc())
}
localFunction()

Additionally, prior to the introduction of JavaScript 1.5, access to your caller and its callers was through the arguments.caller and arguments.caller.callee objects. For example, assume the following scenario:

function a (){ b() }
function b (){ c() }
function c (){ d() }
function d (){getStack() }
a()

to print a list of the names of all the functions from the original caller to the current point of execution was achieved by writing the getStack function as follows

 function getStack(){
      var f = arguments.caller
      var stack = "Stack trace:";
      while (f != null){
        stack += "\r\n" + f.callee.toString().split("{")[0];
        f = f.caller
      }
      Console.Writeln(stack)
   }

With JavaScript 1.5 arguments are no longer available and a "name" property has been added. The equivalent function is now:

function getStack(){
      var f = getStack.caller
      var stack = "Stack trace:";
      while (f != null){
        stack += "\r\n function " + f.name + "()";
        f = f.caller;
      }
      Console.Writeln(stack)
   }

The Mozilla web site also refers to the fact that where there is recursion in functions, then an infinite loop occurs with this code. See http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:caller. If the original environment showed:

var go = true
function a (){ b() }
function b (){
   if (go){ go = false; a() }
   else{ c() }
}
function b (){ c() }
function c (){ d() }
function d (){getStack() }
a()
 

then an infinite loop resulted in the function trying to print the stack trace. To avoid this, add script that stops after a certain amount of loops

Examples

The following code checks the value of a function's caller property.

function myFunc() {
      if (myFunc.caller == null) {
            alert("The function was called from the top!")
      } else alert("This function's caller was " + myFunc.caller)
}

See also

Function: arguments