views:

11249

answers:

7
function main()
{
   Hello();
}

function Hello()
{
  // how do you find out the caller is function 'main'?
}

Is there a way to find out call stack at all?

Thanks.

+17  A: 
function Hello()
{
    alert("caller is " + arguments.callee.caller.toString());
}
Greg Hewgill
Interesting, but I could make it work only in IE (6). It doesn't work in FF3, Opera 9, Safari 3...
PhiLho
What do you mean by "doesn't work"? Exception? Doens't show anything? I just tested on FF 3.6.10 and of course it works. Test page here: http://jsfiddle.net/Gq8Wd/
Protron
Woah, it puts the whole function to string? ... My mind is buzzing with javascript tricks that might be, if it isn't protected.
`arguments.callee.caller.name` will get the function's name.
Rocket
+3  A: 

Its safer to use arguments.callee.caller since arguments.caller is deprecated...

Pablo Cabrera
+1  A: 

arguments.callee.caller.nom

+1  A: 
function Hello() {
    alert(Hello.caller);
}
Shadow2531
+4  A: 

to recap (and make it clearer) ...

this code:

function Hello() {
    alert("caller is " + arguments.callee.caller.toString());
}

is equivalent to this:

function Hello() {
    alert("caller is " + Hello.caller.toString());
}

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 u decide to refactor the name of the invoked function (Hello), you would have to change all its occurencies :(

nourdine
very cool, thanks
Matt Refghi
+2  A: 

StackTrace

You can find the entire stack trace using browser specific code. The good thing is someone already made it; here is the project code on GitHub.

But not all are good news:

  1. It is really slow to get the stack trace so be careful (read this for more).

  2. You will need to define function names for the stack trace to be legible. Because if you have a code like this:

    var Klass = function kls() {
       this.Hello = function() { alert(printStackTrace().join('\n\n')); };
    }
    new Klass().Hello();
    

    Google Chrome will alert ... kls.Hello ( ... but most browsers will expect a function name just after the keyword function and will treat it as an anonymous function. An not even Chrome will be able to use the Klass name if you don't give the name kls to the function.

    And by the way, you can pass to the function printStackTrace the option {guess: true} but I didn't find any real improvement by doing that.

  3. Not all browsers gives the same information. That is, parameters, code column, etc.


Caller Function Name

By the way, if you only want the name of the caller function (in most browsers, not IE) you can use:

arguments.callee.caller.name

But note that this name will be the one after the keyword function and I found no way (even on Google Chrome) to get more than that without getting the hole function code.


Caller Function Code

And summarizing the rest of the best answers (by Pablo Cabrera, nourdine, and Greg Hewgill). The only cross-browser and really safe thing you can use is:

arguments.callee.caller.toString();

Which will show the code of the caller function. Sadly, that is not enough for me, and that is why I give you tips for the StackTrace and the caller function Name (although they are not cross-browser).

Protron
+1  A: 

You can get full stacktrace:

arguments.callee.caller
arguments.callee.caller.caller
arguments.callee.caller.caller.caller

Until caller != null

ale5000