How do you find out the caller function in JavaScript?

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

    I wanted to add my fiddle here for this:

    http://jsfiddle.net/bladnman/EhUm3/

    I tested this is chrome, safari and IE (10 and 8). Works fine. There is only 1 function that matters, so if you get scared by the big fiddle, read below.

    Note: There is a fair amount of my own "boilerplate" in this fiddle. You can remove all of that and use split's if you like. It's just an ultra-safe" set of functions I've come to rely on.

    There is also a "JSFiddle" template in there that I use for many fiddles to simply quick fiddling.

提交回复
热议问题