MetalFoxDoS
MetalFoxDoS

Reputation: 409

Get the name of a method stored in a variable

I have some trouble with getting the name of a method stored in a variable... Here is an exemple of what I want :

Function MyObject(){
    this.actualMethod = this.thatName;
}

MyObject.prototype.thatName = function(){}

MyObject.prototype.getActualMethodName = function(){
    return this.actualMethod.name; /* This doesn't work since the function itself is anonymous, so this.actualMethod.name doesn't work... I want it to return "thatName" */
}

I tried to navigate through the prototype with my console, in vain... Is there a way to do this ?

Upvotes: 0

Views: 38

Answers (1)

Razem
Razem

Reputation: 1441

You need to name the function:

MyObject.prototype.thatName = function thatName() {};

Or, as you mentioned, you can find its name in the prototype (but I wouldn't suggest you to do it):

for (var key in MyObject.prototype) {
  if (MyObject.prototype[key] === this.actualMethod) {
    return key;
  }
}

But why do you need this? Maybe there could be a better solution.

Upvotes: 2

Related Questions