In javascript, how do I find out the name of a function from within that function?

Posted by roryhewitt on Stack Overflow See other posts from Stack Overflow or by roryhewitt
Published on 2010-05-18T23:35:18Z Indexed on 2010/05/18 23:40 UTC
Read the original article Hit count: 351

Filed under:
|
|

The title should make it clear.

Is it possible from within a function to determine the name of that function?

Basically, I'm adding some debugging code to a number of functions, and I'd like to be able to simply add the following line inside every function:

if (global_debugOn) alert("Processing function " + function-name);

How can I get 'function-name'?

Yes, obviously I could simply type in the function name (after all I'm typing in the whole alert bit), but that's a hassle to do, especially if there is a nice simple way of retrieving it dynamically. Plus, as function names change during development, I'd like to keep it up-to-date...

I hoped that maybe the arguments attribute might hold this (e.g. arguments[0], like in C), but I couldn't get it to work. I'm not even sure if arguments works anyway.

Thanks!

Rory

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about function