How do you find out the caller function in JavaScript?

前端 未结 30 1501
粉色の甜心
粉色の甜心 2020-11-21 17:46
function main()
{
   Hello();
}

function Hello()
{
  // How do you find out the caller function is \'main\'?
}

Is there a way to find out the call

相关标签:
30条回答
  • 2020-11-21 17:59

    You can use Function.Caller to get the calling function. The old method using argument.caller is considered obsolete.

    The following code illustrates its use:

    function Hello() { return Hello.caller;}
    
    Hello2 = function NamedFunc() { return NamedFunc.caller; };
    
    function main()
    {
       Hello();  //both return main()
       Hello2();
    }
    

    Notes about obsolete argument.caller: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/caller

    Be aware Function.caller is non-standard: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller

    0 讨论(0)
  • 2020-11-21 18:00

    Just want to let you know that on PhoneGap/Android the name doesnt seem to be working. But arguments.callee.caller.toString() will do the trick.

    0 讨论(0)
  • 2020-11-21 18:02

    Try the following code:

    function getStackTrace(){
      var f = arguments.callee;
      var ret = [];
      var item = {};
      var iter = 0;
    
      while ( f = f.caller ){
          // Initialize
        item = {
          name: f.name || null,
          args: [], // Empty array = no arguments passed
          callback: f
        };
    
          // Function arguments
        if ( f.arguments ){
          for ( iter = 0; iter<f.arguments.length; iter++ ){
            item.args[iter] = f.arguments[iter];
          }
        } else {
          item.args = null; // null = argument listing not supported
        }
    
        ret.push( item );
      }
      return ret;
    }
    

    Worked for me in Firefox-21 and Chromium-25.

    0 讨论(0)
  • 2020-11-21 18:02

    If you really need the functionality for some reason and want it to be cross-browser compatible and not worry for strict stuff and be forward compatible then pass a this reference:

    function main()
    {
       Hello(this);
    }
    
    function Hello(caller)
    {
        // caller will be the object that called Hello. boom like that... 
        // you can add an undefined check code if the function Hello 
        // will be called without parameters from somewhere else
    }
    
    0 讨论(0)
  • 2020-11-21 18:03

    In both ES6 and Strict mode, use the following to get the Caller function

    console.log((new Error()).stack.split("\n")[2].trim().split(" ")[1])
    

    Please note that, the above line will throw an exception, if there is no caller or no previous stack. Use accordingly.

    To get callee (the current function name), use:

    console.log((new Error()).stack.split("\n")[1].trim().split(" ")[1]) 
    
    0 讨论(0)
  • 2020-11-21 18:04

    I usually use (new Error()).stack in Chrome. The nice thing is that this also gives you the line numbers where the caller called the function. The downside is that it limits the length of the stack to 10, which is why I came to this page in the first place.

    (I'm using this to collect callstacks in a low-level constructor during execution, to view and debug later, so setting a breakpoint isn't of use since it will be hit thousands of times)

    0 讨论(0)
提交回复
热议问题