MZ On aliasing/overriding a method.

Alright so it's been 2 months since I last even looked at JS and I forgot most of the advance stuff, the basics slowly coming back. So there's an idea as to where I am with it.

In anycase, tracing my steps with my old scripts, I noticed I never overridden a method that calls another method. It's always been just one method. I suppose 'past me' was doing this on purpose for this very reason, but 'past me' didn't think about future me needing to know this later. (lol)

Taking out the function body to keep it clean/short, how do I override this? apply the alias to the call as well or does only the first method matter?:

Code:
Game_Actor.prototype.performAction = function(action) {
    Game_Battler.prototype.performAction.call(this, action);
....
}

The way I had done it for single methods were the syntax of:
Code:
(function(alias) {
    function header.. {
        alias.apply(....);
    };
})(function header...);
I think both if those can work, depening on the structure.

ES5 mostly use the top one (which I use as well), ES6 and other
people that are good uses the bottom one, but I dont quite get
that part.

the first one personally is more userfriendly, but it depends how
you would alias a function, both works, but the one you choose
is the one you feel most comfortable with.
Hi there!

To alias it properly you can do this:
JavaScript:
const Game_Actor_performAction = Game_Actor.prototype.performAction
Game_Actor.prototype.performAction = function(action) {
    Game_Actor_performAction.call(this, action)
    // Do your stuff
}

To override, do this:
JavaScript:
Game_Actor.prototype.performAction = function(action) {
    // Do your stuff
}
I personally do it like how Eliaquim does it, and your first code block. If it's a plugin only for my project, I sometimes just override existing methods.
I do it like Eli does as well, only difference is that I always have a TLB object in my plugins that stores the aliases I write so that other people can see my code if they need to but I still avoid polluting the namespace.
Ok thank you, so it's the main method that matters(I even forgot the terminology lol, oh well, at least reading JS docs is very engaging).
Ok thank you, so it's the main method that matters(I even forgot the terminology lol, oh well, at least reading JS docs is very engaging).
It can help to reframe it. What you're doing isn't *aliasing*, per se; you're not giving the function a different name. What you're doing is storing the original function inside a new variable that you can use to refer to it later, then replacing the original with your own. You can do this because the body of a function is an object in Javascript like anything else, so you can assign it to a variable or provide it as an argument to another function.

Original thread: https://forums.rpgmakerweb.com/threads/on-aliasing-overriding-a-method.144310/