1

Possible Duplicate:
How do you find out the caller function in JavaScript?

If I put console.log() inside a function, is there anything that I can put inside the console.log() statement to find out where the function was called from? Or is there some other way to find out how a function is called?

I'm trying to understand a relatively complex (complex for someone of my experience) piece of code, and it's hard to figure out what is calling a certain function. Thanks.

Community
  • 1
  • 1
BrainLikeADullPencil
  • 11,313
  • 24
  • 78
  • 134

2 Answers2

8

Perhaps you are looking for console.trace()? It prints out a stack trace.

Here's the Firebug documentation on the function (which is also supported in Webkit):

Just call console.trace() and Firebug will write a very informative stack trace to the console. Not only will it tell you which functions are on the stack, but it will include the value of each argument that was passed to each function. You can click the functions or objects to inspect them further.

Community
  • 1
  • 1
Ivan
  • 10,052
  • 12
  • 47
  • 78
2

use it like this

console.log(arguments.callee.caller.toString())
hish
  • 145
  • 4