How do you find out the caller function in JavaScript?
Note that this solution is deprecated and should no longer be used according to MDN documentation
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller
function Hello()
{
alert("caller is " + Hello.caller);
}
Note that this feature is non-standard, from Function.caller
:
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
The following is the old answer from 2008, which is no longer supported in modern Javascript:
function Hello()
{
alert("caller is " + arguments.callee.caller.toString());
}
How do you find out the caller function in JavaScript when use strict is enabled?
For what it's worth, I agree with the comments above. For whatever problem you're trying to solve, there are usually better solutions.
However, just for illustrative purposes, here's one (very ugly) solution:
'use strict'
function jamie (){
var callerName;
try { throw new Error(); }
catch (e) {
var re = /(\w+)@|at (\w+) \(/g, st = e.stack, m;
re.exec(st), m = re.exec(st);
callerName = m[1] || m[2];
}
console.log(callerName);
};
function jiminyCricket (){
jamie();
}
jiminyCricket(); // jiminyCricket
I've only tested this in Chrome, Firefox, and IE11, so your mileage may vary.
How can I find the calling function in JavaScript?
Like this?
function testOne() {
console.log("Test 1");
logTest();
}
function testTwo() {
console.log("Test 2");
logTest();
}
function logTest() {
console.log("Being called from " + arguments.callee.caller.toString());
}
testOne();
testTwo();
If you use 'use strict';
in your JavaScript file, you need to comment/remove it, because otherwise you'll get something like this:
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
Determine calling function in javascript
Each function has a caller
property defined.
From https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/caller:
function myFunc() {
if (myFunc.caller == null) {
return ("The function was called from the top!");
} else
return ("This function's caller was " + myFunc.caller);
}
}
The Function.caller
property is not part of the ECMA3 standard but it's implemented across all major browsers, including IE and Firefox.
If you're using an anonymous function, you can still access the caller
property via the arguments.calee
property:
function() {
if (arguments.callee.caller == null) {
return ("The function was called from the top!");
} else
return ("This function's caller was " + arguments.callee.caller);
}
}
Note that this code is accessing the current function, and then referencing the same non-standard caller
property on it. This is distinct from using the deprecated arguments.caller
property directly, which is not implemented in some modern browsers.
Is there a way to get the name of the caller function within the callee?
You used to be able to do arguments.caller.name
, but this is deprecated in Javascript 1.3.
arguments.callee.caller.name
(or just showMe.caller.name
) is another way to go. This is non-standard, and not supported in strict mode, but otherwise currently supported in all major browsers (ref).
Related Topics
Why Would a JavaScript Variable Start With a Dollar Sign
Resizing an Iframe Based on Content
Is There a "Null Coalescing" Operator in JavaScript
JavaScript Raises Syntaxerror With Data Rendered in Jinja Template
How to Check If a String "Startswith" Another String
Build Tree Array from Flat Array in JavaScript
Is There a Standard Function to Check For Null, Undefined, or Blank Variables in JavaScript
"Uncaught Syntaxerror: Cannot Use Import Statement Outside a Module" When Importing Ecmascript 6
How to Enable Cors in Angularjs
Xml Parsing of a Variable String in JavaScript
Detecting an "Invalid Date" Date Instance in JavaScript
Regular Expression to Match Non-Ascii Characters
How to Convert a String to Boolean in JavaScript
How to Iterate Over a JavaScript Object
Pass Correct "This" Context to Settimeout Callback
How to Debug My JavaScript Code