views:

51

answers:

1

What's the best way to find the function that's calling the function?

For example, if I have

function first(){
  doSomething();
}

// Lots of code

function doSomething(){
  alert('Somehow, I know that' + ________ + 'function called me...');
  alert('Boink, hit an error, but now you know what function called me');
}

Is there any way to get that calling function? Thanks!

+3  A: 

Replace __ with doSomething.caller.

Cory Petosky
That's exactly what I was looking for, thanks. What if it's undefined, but every instance I call the function in is within another function? Does it mean I'm going crazy?
Matrym
Are you using IE? It doesn't support Function.caller.
Cory Petosky
I should add that both `caller` and `callee` have been removed in ECMAScript 5, so be careful if you're planning to use this code in the long term.
casablanca
@Cory - I'm using chrome/ffx for debugging / @casa - what's the alternative?
Matrym