function main()
{
Hello();
}
function Hello()
{
// How do you find out the caller function is 'main'?
}
Is there a way to find out the call stack at all?
function main()
{
Hello();
}
function Hello()
{
// How do you find out the caller function is 'main'?
}
Is there a way to find out the call stack at all?
Try the following code:
Worked for me in Firefox-21 and Chromium-25.
here is a function to get full stacktrace:
Another way around this problem is to simply pass the name of the calling function as a parameter.
For example:
Now, you could call the function like this:
My example uses a hard coded check of the function name, but you could easily use a switch statement or some other logic to do what you want there.
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.
I think the following code piece may be helpful:
Execute the code:
The log looks like this:
Just want to let you know that on PhoneGap/Android the
name
doesnt seem to be working. Butarguments.callee.caller.toString()
will do the trick.