How do you find out the caller function in JavaScript?

前端 未结 30 1490
粉色の甜心
粉色の甜心 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 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

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

提交回复
热议问题