How to get the name of the caller of a function?!

Hi everyone!

I'm trying something with no success 🙁

Let's look at the function, for example:


Game_Actor.prototype.isActor = function() {
return true;
};




I want to know the name of the function who called this one:


So I looked up and it seems that what I need is "arguments.callee.caller.name" so:
 


var alias = Game_Actor.prototype.isActor
Game_Actor.prototype.isActor = function() {
alias.call(this);
console.log(arguments.callee.caller.name);
};



but then I can't access the name!! nothing appears 🙁  I can access the arguments.callee.caller (it will return the code inside the function) but nothing more!!
Some help, here?!
This looks like very bad practice to me. Why would you want to know that?
This looks like very bad practice to me. Why would you want to know that?


Developing a plugin right now and have to alias a lot of functions! If I knew the caller... then I would only need to alias one, and do different things according to the caller 😛  I thought it would be good performance wise. But if it is bad practice I'll do it the other way around! xD
arguments.callee is forbidden in strict mode, which i highly encourage you to use, so i suspect there's a reason for that.


Also, i would try to keep the number of aliased functions low, because they increase the possibility of incompatibilities with other plugins (though sometimes there's nothing you can do about that). Try to handle as much as possible inside your own plugin and if you absolutely need to add new properties/functions to existing classes, at least prefix them with your name or something unique.
"alias" is bad!? I thought "override" was bad! :/

 
It's not bad, but using call/apply comes with a performance cost and extending base functions always brings the risk of incompatibilities. It's good practice to stay inside your own plugin, as much as possible (also enforces OOP encapsulation, which is another good practice).
arguments.callee is forbidden in strict mode, which i highly encourage you to use, so i suspect there's a reason for that.



The reason is merely because it's very slow as it prevents optimizations, for this reason trying to access it will throw a TypeError.


in Chrome/Webkit there's a console.trace method which can be invoked like console.log but contains the stacktrace,


this plus the fact that you can name your functions make it easy to debug

Code:
Game_Actor.prototype.isActor = function isActor() {
    console.trace("I am the original");
    return true;
};

var alias = Game_Actor.prototype.isActor
Game_Actor.prototype.isActor = function myOverriddenIsActor() {
    alias.call(this);
    console.trace("Hey, I just met you, and this is crazy, but here' s my stacktrace, so look me maybe")
};

Original thread: https://forums.rpgmakerweb.com/threads/how-to-get-the-name-of-the-caller-of-a-function.55792/