copenndthagen
copenndthagen

Reputation: 50790

How can I find the calling function in JavaScript?

I have a function in JS which is getting called from multiple places..

Now I am testing this page on an iPad and hence find debugging a bit hard.

Can I find out by some way (print on console) from where my function is getting called from ?

Upvotes: 16

Views: 21182

Answers (4)

some_groceries
some_groceries

Reputation: 1192

Do not use function.caller it is non standard and is not recommended.

Instead use the browser console and put break points on your function check the call stack, Here is a screenshot of chrome debugger console

enter image description here

Upvotes: 3

James
James

Reputation: 5169

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

Upvotes: 22

Nishant Kumar
Nishant Kumar

Reputation: 6093

Want to Detail about caller Function:

function nishant(){  // Caller function 
   kumar();
}nishant();

function kumar(){ // Callee
 console.log("This functiona is being called by " + arguments.callee.caller.toString());
}  

In place of arguments.callee.caller.toString() we can also use functionName.caller

Example:

function nishant(){  // Caller function 
   kumar();
}nishant();

function kumar(){ // Callee
 console.log("This functiona is being called by " + kumar.caller);
}  

Output: will be same in both of the above case

This functiona is being called by function nishant()
{
kumar();
}

Upvotes: 2

SquareFeet
SquareFeet

Reputation: 641

A simple way I like to use is arguments.callee.caller.name.

Say you wanted to know what was calling a function called myFunction:

function myFunction() {
    console.log(arguments.callee.caller.name);
    /* Other stuff... */
}

The browser support for this isn't that great, though, so you could use arguments.callee.caller.toString() instead. Note that this will give you back the contents of the function that called myFunction, so you will need to dig out the function name from that yourself.

Or, you could use a nice stack trace function like this:

function getStack(){
    fnRE  = /function\s*([\w\-$]+)?\s*\(/i;
    var caller = arguments.callee.caller;
    var stack = "Stack = ";
    var fn;
    while (caller){
        fn = fnRE.test(caller.toString()) ? RegExp.$1 || "{?}" : "{?}";
        stack += "-->"+fn;
        caller = caller.arguments.callee.caller;
    };
    return stack;
}

Stack Trace via http://www.amirharel.com/2010/01/25/using-caller-and-callee-for-stack-trace/

Upvotes: 14

Related Questions