How do you find out the caller function in JavaScript?

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

    here is a function to get full stacktrace:

    function stacktrace() {
    var f = stacktrace;
    var stack = 'Stack trace:';
    while (f) {
      stack += '\n' + f.name;
      f = f.caller;
    }
    return stack;
    }
    

提交回复
热议问题