kalan kalan - 11 months ago 81
Javascript Question

Get function name in JavaScript

Does anyone know if there is a way to get JavaScript function name. For example I got a function like

function test1(){

I have it in my head section. Then I create an object obj1 and put my function there

obj1.func = test1;

When I call a method in obj1 object, do I have any way to get my function name (test1) inside of this method, except parsing the source (
) of the function.

Answer Source
function test() {  alert(arguments.callee.name); } 
b = test; 

outputs "test" (in Chrome, Firefox and probably Safari). However, arguments.callee.name is only available from inside the function.

If you want to get name from outside you may parse it out of:


but I think name property of function object might be what you need:


this however does not seem work for IE and Opera so you are left with parsing it out manually in those browsers.