Let's Talk About Aliases

i have been wondering now for some time, what would happen if multiple plugins aliased the same method? would the latter alias take precedence over the first? do they both process? would the default functionality of the aliased method process multiple times?

what actually takes place when multiple aliases from different plugins occur?
You'll end up with one gigantic function and two variables having the same value as that function.
So if you have a function {
A (standard rpg_whatever.js code)
}
function {
B (one plugin)
}
function {
C (another plugin)
}
you will end up with one long function consisting of either {
A
B
C
}
or {
A
C
B
}
depending on the plugin order.
when the code compiles at run-time then, it simply appends all of the new functionality to the end of the default code, in order of the plugin's position in the plugin folder?
Not plugin folder. The json file that is responsible for plugin settings, because that one contains plugin ordering in your project. But otherwise you got it right. Of course I may be wrong, but this is how variable definition works, so I don't see why would I be wrong 😀
in theory then, i could create five separate plugins and let's say three of them aliased the same method; so long as i did not tamper with the processing by including returns or anything like that, there will be zero compatibility issues?
if you need more understand , try open index.html
you will see order libs execution.
you can see src="js/plugins.js"></script> at last line.
So yes all plugin alias compile in oerder , and also in the order of the plugin list.
So if you first plugin replace a function code, and your second plugin replace the same function.
Its the function of the last plugin only will be valide in game.
Order have importance only if it replace the same function witout alias for example.

The good way for made plugin , its add all rmmv core alias in a variable.
PHP:
 myPluginName._rmmvAlias.DataManager_createGameObjects = DataManager.createGameObjects;
    DataManager.createGameObjects = function() {


        myPluginName._rmmvAlias.DataManager_createGameObjects.call(this);

    };
it is important to keep it clean and store all properly otherwise you will cry during the process of debugging, and minification.

you must avoid aliases in a updates function, otherwise your performance will be affected.
Actually, @Jonforum you are wrong here.
Upon aliasing the functions don't get overwritten, but build up on one another. Yanfly has plugins built upon this, aliasing functions she has already aliased in battle engine core for example.
So, @dbchest, you are right. In theory there should be no compatibility issues if you make these plugins well. As for returns though, these get overruled upon new aliasing, so only the last function matters here. This is what breaks plugins.
Actually, @Jonforum you are wrong here.
Upon aliasing the functions don't get overwritten, but build up on one another. Yanfly has plugins built upon this, aliasing functions she has already aliased in battle engine core for example.
So, @dbchest, you are right. In theory there should be no compatibility issues if you make these plugins well. As for returns though, these get overruled upon new aliasing, so only the last function matters here. This is what breaks plugins.
I may be misreading with my rotting English,
But from what I understood, he wants to know if the order has an important.
Yes.
These always the last function in the order of the pluginLoader that is rendered.
The others are crushed.
for alias , its replace the target function, and you need call original.
my English is bad when I am tired, it may be that I do not express well.
but great tuto here for very good explain.
If you don't believe me, here is a simple proof.
Code:
alias = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    alias.call (this);
    console.log ("one");
};
aliass = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    aliass.call (this);
    console.log ("two");
    return 200;
};
aliasss = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    aliasss.call (this);
    console.log ("three");
};
I am aliasing one function 3 times. If what you say is right, the console will only have one input... But it will have three.
The only thing that gets overridden are returns. But everything else stacks. I'll explain to you through PM in French.
If you don't believe me, here is a simple proof.
Code:
alias = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    alias.call (this);
    console.log ("one");
};
aliass = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    aliass.call (this);
    console.log ("two");
    return 200;
};
aliasss = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    aliasss.call (this);
    console.log ("three");
};
I am aliasing one function 3 times. If what you say is right, the console will only have one input... But it will have three.
The only thing that gets overridden are returns. But everything else stacks. I'll explain to you through PM in French.

yes, I had not expressed myself well.
What I meant was these
PHP:
// plugin 1
alias = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    alias.call (this);
    console.log ("one");
};
//plugin 2
alias = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    alias.call (this);
    console.log ("two");
    return 200;
};
//plugin 3
alias = Window_Gold.prototype.windowWidth;
Window_Gold.prototype.windowWidth = function() {
    alias.call (this);
    console.log ("three");
};
it will only plugin 3 will work and cruch other.
But sorry it just me I have read wrongly. 🤘
I had misunderstood the question. I go out lol

when aliases with different name, everything accumulates here because you store them in deferred variable , so order does not matter.
Well, of course it will get messed up if you alias with the same name 😀 You'd be rewriting one function again and again.
Aliasing with different name still puts an emphasis in order in terms of returns though. Because everytime you alias a new function, the old returns get overwritten. The reason why is simply because of calling.
so this is good information for me. what has been posted here in addition to some tests i ran myself has really helped me clear up the questions i had about aliasing, so i wanted to thank you both for contributing. that being said, do either of you have any special considerations that should be kept in mind when doing aliases, or does what we have discussed pretty much sum up the important stuff?
Always make sure to create variables with different names and don't use return, that is the only thing I can say 🙂
i have another question. if i were to place my code above the call to the original method, could i squeeze new functionality in that way if the default code utilizes a return? see example below:

Code:
var _Game_Player_canMove = Game_Player.prototype.canMove;
Game_Player.prototype.canMove = function() {
    my code here
    _Game_Player_canMove.call(this);
};

i used this method for the example because it utilizes a return at the end of the function. if i placed my code above the call to the original method, in theory it would process before the original method and everything should work fine, right?
Why are you asking us? The fastest method to find out is to try it yourself 😀
From the looks of it, it looks doable.
someone with similar concerns may find this thread. 😀

edit:
that did not work and i do not understand why.
yes the order where you call the original function have importance.
Example here of one my code.
i call Befor my plugin initialisation because a have important element.
after a call the DB rmmv.
And after i add a another proprety.
this will depend of what you are target.
PHP:
    pixiSSA._rmmvAlias.DataManager_createGameObjects = DataManager.createGameObjects;
    DataManager.createGameObjects = function() {
        pixiSSA.core_Initialized || pixiSSA.initialize_before(); // initialise pixiSSA plugin befor DataManager
        pixiSSA._rmmvAlias.DataManager_createGameObjects.call(this); // call original rmmv data manager builder
        Object.defineProperty(pixiSSA, 'core_Initialized', { value: true }); // prevent multi pixiSSA initialize on scene reset from rmmv (ex: title, battle)
    };
@dbchest Well, I see why it is not working.
The reason is calling.
When a function calls another function, let's say A calls B, the return value of B is only executed inside A, not the actual return value of A.

I would compare it to this example:
Code:
var example = function () {
example2.call (this)
}
and example 2 is some return function.
However, the return value of example2 is not stored in a variable, so we have called a function, but cannot work with the value.
To be able to use that any further, we need this:
Code:
var example = function () {
var x = example2.call (this)
}
Now we have the return value of example2 stored in a variable and we can work with it.
However, I am not entirely sure if I could recommend that with aliasing 😀
@Poryg you are blowing my mind and tests must be run; standby. 😛

edit:
i feel like this is inception, haha. we are three dreams deep and i am losing track of what is real. it makes sense what you explained above; the return from the original method only returns that value to our alias, so in order to use that value we must store it or return it at the time we call it (i returned it when calling it and it worked like a charm).

Code:
var _Game_Player_canMove = Game_Player.prototype.canMove;
Game_Player.prototype.canMove = function() {
    myCodeHere;
    return _Game_Player_canMove.call(this);
};
Aliasing is replacing a function with another but retaining a reference to the original function. The original function is invoked by the replacement function. A method with multiple replacements will be processed from newest to oldest.

Hypothetical pluginA aliases the original someMethod()and then pluginB comes along and aliases pluginA's replacement for someMethod() and adds its own. When someMethod() is invoked pluginB's replacement will be called first, followed by pluginA's replacement then finally the original function. The original function is not executed multiple times.

Spoiler: Source
Code:
var someObj = {};

someObj.someMethod = function () {
    // Original function.
};

/* Then in pluginA you might see... */

var aliasA = someObj.someMethod;

someObj.someMethod = function () {

    // Invoke the original function.

    aliasA.call(this);

    // Do whatever stuff for pluginA.
};

/* Then in pluginB you might see...*/

var aliasB = someObj.someMethod;

someObj.someMethod = function () {

    // Invoke the original function.

    aliasB.call(this);

    // Do stuff for pluginB.
}

The replacement function will need to pass on arguments to the original function as needed and any return value. For example, what if someMethod() was expecting one or more arguments? In that case I might choose to use apply() instead of call().

Spoiler: Source
Code:
var someObj = {};

someObj.someMethod = function (arg1, arg2) {

    // Original function.
   
    return arg1 + arg2;
};

/* In pluginC */

var aliasC = someObj.someMethod;

someObj.someMethod = function (arg1, arg2) {

    // Do something for pluginC.

    return aliasC.apply(this, arguments);
}

Sometimes a replacement function needs to know the return value of the original so invoking the original first and storing the value is one way to handle it. The replacement function needs to return the correct value type just as if it were the original.

Spoiler: Source
Code:
var someObj = {};

someObj.someMethod = function (arg) {

    // Original function.
   
    return someValue;
};

/* In pluginD */

var aliasD = someObj.someMethod;

someObj.someMethod = function (arg) {

    // Invoke the original, passing any arguments.

    var returnValue = aliasD.call(this, arg);

    // Inspect the return value and do as needed.

    if (returnValue) {
        // TODO
    }

    // Remember to return the correct value.

    return returnValue;
}

I prefer to use an immediately invoke function expression (IIFE) to save a reference to the original function so I don't have to write some long variable name as an alias but do what works best for you.

Spoiler: Source
Code:
var someObj = {};

someObj.someMethod = function () {
    // Original function.
};

someObj.someMethod = (function (original) {
    return function () {
        original.call(this);

        // Do whatever I needed.
    };
}(someObj.someMethod));

Original thread: https://forums.rpgmakerweb.com/threads/lets-talk-about-aliases.86475/