Is there any downside for aliasing in JavaScript

Hello everyone,

When I was writing my first plugin yesterday, I have gone through several other plugins to see how to write the code properly.

In RGSS, I am always aliasing method to make it less likely to crash with other plugin. In JavaScript, I saw Yanfly's plugin doing alias with something like this:

Wora_SkillHPCondition_GameBatBase_meetSkC = Game_BattlerBase.prototype.meetsSkillConditions;Game_BattlerBase.prototype.meetsSkillConditions = function(skill) {// do something here// aliasreturn Wora_SkillHPCondition_GameBatBase_meetSkC.call(this, skill);}(This is taken from my first plugin here: http://forums.rpgmakerweb.com/index.php?/topic/49474-skill-hp-condition-enable-this-skill-when-hp-is-higher-lower-than-50/)

I have also discovered that if the function you're aliasing ends with 'return', you have to use 'return' with aliased function name. It will crash if I don't put 'return' in the code above.

Is there any downside for doing this ? If there is none, I think we should promote the use of aliasing since I have seen many plugin developers just copy the core function and edit in their own plugin, which can make it crash with other plugin very easily.
Only problem I see with that is that you're creating a global method called Wora_SkillHPCondition_GameBatBase_meetSkC.

Usually we save our aliased methods on local variables by using an anonymous function.
Unlike ruby, javascript doesn't return anything unless it's explicitly told to, so you would need to remember to return it.


I think it's also good practice to return in general.
Only problem I see with that is that you're creating a global method called Wora_SkillHPCondition_GameBatBase_meetSkC.

Usually we save our aliased methods on local variables by using an anonymous function.
Thank you Hudell 😀 The original code is wrapped like this o_o

(function() {....})();Is this what you mean by anonymous function ? or should I use:

Code:
var Wora_SkillHPCondition_GameBatBase_meetSkC = Game_BattlerBase.prototype.meetsSkillConditions;
Unlike ruby, javascript doesn't return anything unless it's explicitly told to, so you would need to remember to return it.

I think it's also good practice to return in general.
That makes JavaScript so confusing lol

Ruby is much easier for beginner in my opinion.
There do are cases where you explicity don't want to alias, though, depending on what you're trying to do.

I.e: if the default method does something that you do not want to happen. Depending on what it is, you can still alias it and reverse what it does after calling it; but some things can't be reversed so neatly.

Sometimes, overriding a default method can actually increase your compatibility, in situations that something in the default method would break whatever your plugin is supposed to do.
Thank you Hudell 😀 The original code is wrapped like this o_o

(function() {....})();Is this what you mean by anonymous function ?
Yes. In this case, the alias will only exist inside that function.
There do are cases where you explicity don't want to alias, though, depending on what you're trying to do.

I.e: if the default method does something that you do not want to happen. Depending on what it is, you can still alias it and reverse what it does after calling it; but some things can't be reversed so neatly.

Sometimes, overriding a default method can actually increase your compatibility, in situations that something in the default method would break whatever your plugin is supposed to do.
You're right Mellye 😀 Though in some plugins I went through yesterday, I can see that aliasing wouldn't harm their compatibility at all.

------------------

@Hudell:

YAY 😀 I did that because I saw other plugins does it lol.

By the way, does that mean I should write outside anonymous function if I am not aliasing ?
You're right Mellye 😀 Though in some plugins I went through yesterday, I can see that aliasing wouldn't harm their compatibility at all.

------------------

@Hudell:

YAY 😀 I did that because I saw other plugins does it lol.

By the way, does that mean I should write outside anonymous function if I am not aliasing ?
Everything you want to be accessible from other plugins needs to be outside anonymous. Everything else you can add inside it. For example: I leave a "Imported" variable outside and another variable with the name of my plugin. Everything else I leave inside an anonymous function, but I then add stuff to that variable with the name of my plugin.
Everything you want to be accessible from other plugins needs to be outside anonymous. Everything else you can add inside it. For example: I leave a "Imported" variable outside and another variable with the name of my plugin. Everything else I leave inside an anonymous function, but I then add stuff to that variable with the name of my plugin.
Thank you Hudell ! 🙂

One question though, I always thought that using generic word for variable name is not a great idea. Like your 'Imported' which some badly written plugin might come by and replace your variable with something else.
The idea is having an Imported variable that everybody uses on the same way.
The idea is having an Imported variable that everybody uses on the same way.
Oh I see 😀 Thank you for clarifying this 🙂

I will add this to my plugin too.
Everything you want to be accessible from other plugins needs to be outside anonymous. Everything else you can add inside it. For example: I leave a "Imported" variable outside and another variable with the name of my plugin. Everything else I leave inside an anonymous function, but I then add stuff to that variable with the name of my plugin.
wait... so if we wrap it like that... put alias inside it. then when we write another plugin... we can use the same alias again?

how about

(function($) {....})(MVCommon);in mv common

inside it i also see $.methodblablabla...
That makes JavaScript so confusing lol

Ruby is much easier for beginner in my opinion.
Actually, once you start doing the more heavy languages, Ruby's the odd one. What Ruby does is, it returns the last variable set, whereas any other language, it returns either nothing or a variable that's explicitly returned. This has the added advantage that you can basically determine yourself what gets returned instead of letting the interpreter or compiler decide for you. Generally, it's good practice to always use a return value when a value needs to be returned.

wait... so if we wrap it like that... put alias inside it. then when we write another plugin... we can use the same alias again?

how about

(function($) {....})(MVCommon);in mv common

inside it i also see $.methodblablabla...
Essentially, what the (function() { })(); and +function() { }(); are doing is, it directly executes a function with a given parameter set if possible.

Essentially you can give the function certain parameters it can accept, and set these in the closing parentheses.

Essentially:

+function($) {}(jQuery);
is functionally the same to:

Code:
var executeScript = function($) {} executeScript(jQuery);
wait... so if we wrap it like that... put alias inside it. then when we write another plugin... we can use the same alias again?

how about

(function($) {....})(MVCommon);in mv common
inside it i also see $.methodblablabla...
 In that case, doing $.method is the same as doing MVCommon.method.

But yes, if you make

(function($) {var method1 = Game_Player.prototype.moveStraight;})(MVCommon);you can create another variable called method1 anywhere else if you want.
edit: Ninja'ed
Gernerally if I alias something I will keep it global, If I am sub classing I place it in a ,module and export it and only assign the public api to the window element. I really hate global objects in javascript.
Gernerally if I alias something I will keep it global, If I am sub classing I place it in a ,module and export it and only assign the public api to the window element. I really hate global objects in javascript.
after quick though... i quite agree with this... i mean the line where "if we alias something... we need to keep it global..."

so people who want to use the 'unaliased' function could still access it. (maybe compatibility patch?)

if we wrap it inside

(function($) {

....

})(MVCommon);

or

(function() {

....

})();

that means the 'unaliased' / 'orginal' method is lost forever...

because it become private variable...??

am i right or i miss something here?

of any way to grab the private variables? (in ruby there's a way to grab private variables / methods)

edit:

also... in moghunter script in ruby... there's a script that like written this:

module X  module Y    alias blablabla_initialize initialize    def initialize(parameter here)         code here         blablabla_initialize(parameter here)    end  endend class something   include X::Yendclass something2   include X::Yendclass something3   include X::Yendclass something4   include X::Yendany way to do that in JS?

especially if there's alias inside the module.

(but if the alias is not supported... how about just the include part) 😀 .

this could be useful if we want to add methods to some object with different constructor but have the same common structure.

for example: most object have note (map, comment, actor, item, etc)
If you want other people to be able to call the old method, you should add the old method to the same class that you took it from.
If you want other people to be able to call the old method, you should add the old method to the same class that you took it from.

hahah , sorry. You always want people to be able to call the old method
Not really "always", but that problem will overriting things is pretty common.

Aliasing in javascript doesn't really change much about aliasing in ruby, you still have a little performance lost (don't go crazy with it anyway, its slower, but not so slow to avoid it), you still keep the old name somewhere (even if it's inside an anonymous function) and all the features of "reusing" are just the same.

They have downsides you may notice when someone actually imports over 300+ plugins than alias Game_Map.prototype.update, but who would be that crazy to do... oh, forget it, rm users are going tol do that eventually...

@estriole...

Something like this probably:

function include(baseClass, includedModule) { for (var p in includedModule) {   if (!baseClass[p]) {     baseClass[p] = includedModule[p];   } }};and then using it like:

include(MyClass.prototype, MyModule.prototype);This isn't "exactly the same" but it will work in most cases...

of course you won't be able to do:

Code:
( (new MyClass()) instanceof MyModule) // nope it's not

Original thread: https://forums.rpgmakerweb.com/threads/is-there-any-downside-for-aliasing-in-javascript.49628/