1

I'm not sure if this is a good idea or not, but I'd like to be able to get the name of a function from within it, while it is being called. The test code I wrote below seems to work, in that it doesn't cause a name clash, but I can't get the name of the function without knowing it first.

function And(){
    return "test";
};
var X = {
    And:  function And(){
        return this.And.name;
    }
};
document.write(X.And());

Is there any way of achieving this that doesn't involve binding the context (i.e. the value of 'this') on the function?

Ultimate Gobblement
  • 1,851
  • 16
  • 23

2 Answers2

2

Simply by using this aguments.callee.name;

try this example :

function myFunc() 
{ 
    return arguments.callee.name; 
}

Note that the arguments.callee will be depricated

Community
  • 1
  • 1
Khalid
  • 4,730
  • 5
  • 27
  • 50
2

Function.caller or arguments.callee.name

However arguments.callee() doesn't work under ES5. And Function.caller is not standard.

JoJo
  • 287
  • 1
  • 4