Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 417155 of Function.caller

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Function/caller
  • Revision title: caller
  • Revision id: 417155
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment replaced dead link to dropbox with kangaxs compatibility tableJavaScript/Reference/Global_Objects/Function/caller Web/JavaScript/Reference/Global_Objects/Function/caller

Revision Content

{{ Non-standard_header() }}

Summary

Returns the function that invoked the specified function.

This property is not part of ECMA-262 Edition 3 standard. It is implemented at least in SpiderMonkey (the JavaScript engine used in Mozilla) (see {{ Bug(65683) }}), V8 (the JavaScript engine used in Chrome) and JScript.

Property of Function
Implemented in JavaScript 1.5
ECMAScript Edition none

Description

If the function f was invoked by the top level code, the value of f.caller is null, otherwise it's the function that called f

This property replaces deprecated arguments.caller.

Notes

Note that in case of recursion, you can't reconstruct the call stack using this property. Consider:

function f(n) { g(n-1) }
function g(n) { if(n>0) f(n); else stop() }
f(2)

At the moment stop() is called the call stack will be:

f(2) -> g(1) -> f(1) -> g(0) -> stop()

The following is true:

stop.caller === g && f.caller === g && g.caller === f

so if you tried to get the stack trace in the stop() function like this:

var f = stop;
var stack = "Stack trace:";
while (f) {
  stack += "\n" + f.name;
  f = f.caller;
}

the loop would never stop.

The special property __caller__, which returned the activation object of the caller thus allowing to reconstruct the stack, was removed for security reasons.

Examples

Example: Checking the value of a function's caller property

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

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

 

Browser Support

Function.caller is currently supported by all major browsers: Firefox, Safari, Chrome, Opera and IE.  ECMAScript extensions compatibility table.

 

{{ languages( { "fr": "fr/Référence_de_JavaScript_1.5_Core/Objets_globaux/Function/caller", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Function/caller" ,"zh-cn":"zh-cn/JavaScript/Reference/Global_Objects/Function/caller"} ) }}

Revision Source

<p>{{ Non-standard_header() }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Returns the function that invoked the specified function.</p>
<p>This property is not part of ECMA-262 Edition 3 standard. It is implemented at least in <a href="/en/SpiderMonkey" title="en/SpiderMonkey">SpiderMonkey</a> (the JavaScript engine used in Mozilla) (see {{ Bug(65683) }}), V8 (the JavaScript engine used in Chrome) and JScript.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Property of <a href="/en/JavaScript/Reference/Global_Objects/Function" title="en/JavaScript/Reference/Global_Objects/Function"><code>Function</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.5</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>none</td>
    </tr>
  </tbody>
</table>
<h3 id="Description" name="Description">Description</h3>
<p>If the function <code>f</code> was invoked by the top level code, the value of <code>f.caller</code> is <code>null</code>, otherwise it's the function that called <code>f</code></p>
<p>This property replaces deprecated <a href="/en/JavaScript/Reference/Functions_and_function_scope/arguments/caller" title="En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/arguments/caller">arguments.caller</a>.</p>
<h3 id="Notes" name="Notes">Notes</h3>
<p>Note that in case of recursion, you can't reconstruct the call stack using this property. Consider:</p>
<pre class="eval">
function f(n) { g(n-1) }
function g(n) { if(n&gt;0) f(n); else stop() }
f(2)
</pre>
<p>At the moment <code>stop()</code> is called the call stack will be:</p>
<pre class="eval">
f(2) -&gt; g(1) -&gt; f(1) -&gt; g(0) -&gt; stop()
</pre>
<p>The following is true:</p>
<pre class="eval">
stop.caller === g &amp;&amp; f.caller === g &amp;&amp; g.caller === f
</pre>
<p>so if you tried to get the stack trace in the <code>stop()</code> function like this:</p>
<pre class="eval">
var f = stop;
var stack = "Stack trace:";
while (f) {
  stack += "\n" + f.name;
  f = f.caller;
}
</pre>
<p>the loop would never stop.</p>
<p>The special property <code>__caller__</code>, which returned the activation object of the caller thus allowing to reconstruct the stack, was removed for security reasons.</p>
<h3 id="Examples" name="Examples">Examples</h3>
<h4 id="Example:_Checking_the_value_of_a_function.27s_caller_property" name="Example:_Checking_the_value_of_a_function.27s_caller_property">Example: Checking the value of a function's <code>caller</code> property</h4>
<p>The following code checks the value a function's <code>caller</code> property.</p>
<pre class="eval">
function myFunc() {
   if (myFunc.caller == null) {
      return ("The function was called from the top!");
   } else
      return ("This function's caller was " + myFunc.caller);
}
</pre>
<p>&nbsp;</p>
<h3 id="Browser_Support">Browser Support</h3>
<p>Function.caller is currently supported by all major browsers:&nbsp;Firefox, Safari, Chrome, Opera and IE. &nbsp;<a class="external" href="http://kangax.github.com/es5-compat-table/non-standard/">ECMAScript extensions compatibility table</a>.</p>
<p>&nbsp;</p>
<p>{{ languages( { "fr": "fr/Référence_de_JavaScript_1.5_Core/Objets_globaux/Function/caller", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Function/caller" ,"zh-cn":"zh-cn/JavaScript/Reference/Global_Objects/Function/caller"} ) }}</p>
Revert to this revision