15

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 ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • 2
    This will help: http://stackoverflow.com/questions/280389/how-do-you-find-out-the-caller-function-in-javascript – Sid Nov 21 '11 at 11:44
  • One more link http://stackoverflow.com/questions/147891/javascript-exception-stack-trace – M S Nov 21 '11 at 11:48
  • Possible duplicate of [How do you find out the caller function in JavaScript?](https://stackoverflow.com/questions/280389/how-do-you-find-out-the-caller-function-in-javascript) – Brian Tompsett - 汤莱恩 Aug 21 '19 at 08:12

4 Answers4

21

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
James
  • 5,137
  • 5
  • 40
  • 80
14

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/

SquareFeet
  • 651
  • 4
  • 16
3

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

some_groceries
  • 1,164
  • 18
  • 38
  • if you are not running javascript in a browser (in nodejs for example) use stack trace https://developer.mozilla.org/en-US/docs/Glossary/Call_stack – some_groceries Jul 23 '19 at 09:12
2

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();
}
Nishant Kumar
  • 5,995
  • 19
  • 69
  • 95
  • *functionName.caller* is a nice one, but [non standard](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller) (according to MDN) – mTorres Dec 15 '17 at 13:15