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?
As far as I know, we have 2 way for this from given sources like this-
arguments.caller
Function.caller
Think u have your answer :).
If you just want the function name and not the code, and want a browser-independent solution, use the following:
Note that the above will return an error if there is no caller function as there is no [1] element in the array. To work around, use the below:
I know you mentioned "in Javascript", but if the purpose is debugging, I think it's easier to just use your browser's developer tools. This is how it looks in Chrome: Just drop the debugger where you want to investigate the stack.
As none of previous answers works like what I was looking for(getting just the last function caller not a function as a string or callstack) I post my solution here for those who are like me and hope this will work for theme:
To recap (and make it clearer) ...
this code:
is equivalent to this:
Clearly the first bit is more portable, since you can change the name of the function, say from "Hello" to "Ciao", and still get the whole thing to work.
In the latter, in case you decide to refactor the name of the invoked function (Hello), you would have to change all its occurrences :(
Try accessing this: