Share the way you Alias functions

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I know that there are other ways of aliasing, but not to the point I understand why. By far, I only know two:

This way:

var Milena = Milena || {}; Milena.params = PluginManager.parameters('DiarySystem'); Milena.DiaryStstem = { someAliasStyle: { Game_Screen: { update: Game_Screen.prototype.update, } } };and this way:

var anAliasbyMilena_GameScreen = Game_Screen.prototype.update;I don't understand much about the first so I rarely use it. I use the second one most. Could anyone explain to me how the first one differs more? I also would like to know, what other alias methods can be used in javascript and how they'd differ from the other ones.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
The main difference is that the first is prettier.
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
/edit: This is assuming, that all of this happens in an anonymous function, otherwise all of these expose the alias.

The first version exposes the alias through the module/namespace, so it's possible to directly call the underlying function from other plugins. This allows to rewrite your aliased functions for a compatibility patch or similar, but you may not want to expose these variables.

Since using .call()/.apply() can be very slow in some browsers (namely Firefox), a different way is to add the alias to the prototype, like this:

Code:
Class.prototype.myHopefullyUniqueNameForAnAliasFn = Class.prototype.fn;Class.prototype.fn = function() {    this.myHopefullyUniqueNameForAnAliasFn();    // do stuff};
This also exposes the alias, but adds it to the prototype (similar to how aliases work in Ruby), which has the added benefit that it can be directly called, since "this" is correctly set. On the downside, this adds additional stuff to the prototype, which doesn't look very nice.
 
Last edited by a moderator:

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
May I ask what those additional values are, in particular?
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
In my example, if you console.log() the prototype, you will see that it has a new attribute called "myHopefullyUniqueNameForAnAliasFn", which gets inherited by all instances of that prototype and all subclasses. If this is done by multiple plugins, it may clog up the log.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I see, that's a new knowledge learned, thanks! In Ruby, there's a method called

unless method_defined?

sort of thing. How will we exactly know if a certain alias exists, so we can do different conditions based on what aliasing was used?
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
Methods are just attributes, so if you want to check if an objects contains a function named "myFunction", you can do like this:

Code:
object["myFunction"];object.myFunction;
Both of these are identical, but the former would work, if your function name contains whitespaces. They return undefined, if the object doesn't contain the given attribute name. Since undefined is a falsy value, you can do this:
Code:
if(object.myFunction) { object.myFunction(); }
Falsy values evaluate to false when used in a boolean context (like "if"). Those values are: false, null, undefined, "", 0, NaN.
 

Ramiro

Now with an army of Mecha-Ralphs!
Veteran
Joined
Aug 5, 2015
Messages
858
Reaction score
364
First Language
Spanish
I usually do:

Code:
(function () { var Game_Party_actors = Game_Party.prototype.actors;  Game_Party.prototype.actors = function () {   return Game_Party_actors.apply(this, arguments); };})();
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
That looks pretty neat. How about the arguments? this refers to the function you've created, but what does the arguments stands for? Shouldn't it be only there if there's something that's actually inside the function as an argument?
 

Ramiro

Now with an army of Mecha-Ralphs!
Veteran
Joined
Aug 5, 2015
Messages
858
Reaction score
364
First Language
Spanish
arguments is a variable on all javascript functions than contains, well a list of the arguments.

function myArgTest() {  console.log(arguments);}myArgTest(); // => []myArgTest("an string"); // => ["an string"]myArgTest(1, 2, 3); // => [1, 2, 3]myArgTest([1]); // => [[1]]It's not an array, it something like an array with length and all, but isn't...

[].slice.call(arguments, 0);Will convert them into one thou...

I use the .apply(this, arguments) just for consistency arround my entire code thou, and if for some crazy reason someone actually adds parameters when aliasing a function...

its like in ruby some scripters do "paranoia aliases":

class Game_Battler    alias my_states states  def states(*args, &block)     my_states(*args, &block)  endendOn ruby 2.0 and beyond, the paranoia is even bigger:

Code:
class Game_Battler    alias my_states states  def states(*args, **kargs, &block)     my_states(*args, **kargs, &block)  endend
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
"arguments" is an array-like object, that's available inside a function and contains all parameters passed to it. "apply" takes an array-like object as its second parameter and will pass it to the function as parameters.


The downside hereby is, that "arguments" isn't actually created, until it's used, which makes "apply" slower than "call". It is, however, useful, if you don't know the exact parameters used by the underlying function or if it can receive any number of parameters. Otherwise, i would use "call" and explicitly state the parameters used.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Ahh, I am getting to understand it. That sounds like the reason why when I made a .apply function, it gave me an error, not unless I do add the arguments inside it. But in .call, I just needed the this and the actual created argument values in the parameters.

Is it safer to use .call then, when you know what values you are working in, rather than apply?
 

Ramiro

Now with an army of Mecha-Ralphs!
Veteran
Joined
Aug 5, 2015
Messages
858
Reaction score
364
First Language
Spanish
yes, doing:

funct.call(this, arg1, arg2, arg3...) or:

funct.call(this)is most of the time a bit faster, the arguments variable's creation on use is not really true, that's implementation defined.

Firefox may do that, but chrome could actually had the arguments variable all along, and IE will work it differently.

Also, arguments is one of the things than work in other way on strict mode.

On es6, I'll probably change the aliases into:

var myNewFunct = function (...args) {  alias.apply(this, args);  // or you can  alias.call(this, ...args);}Because the arguments variable is deprecated on ES6...
 
Last edited by a moderator:

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
// Two ways:


var oldCoreClassPrototypeFunctionNameMethod = CoreClass.prototype.functionName;


CoreClass.prototype.functionName = function(args) {


// my crap


oldCoreClassPrototypeFunctionNameMethod.call(this, args);


}


// ES6:


MyClass extends Some_Class {


constructor() {


super();


}


someClassFunction() {


super.someClassFunction();


// my crap


}


}
 

Sulsay

Villager
Member
Joined
Nov 25, 2015
Messages
25
Reaction score
7
First Language
Dutch
Primarily Uses
RMMV
If you're not going to expose the method that you're overriding anyway, why use long an unreadable names for the alias? I never understood.

I do this:

Code:
(function () {  var moveByInput = Game_Player.prototype.moveByInput;        Game_Player.prototype.moveByInput = function () {            // The current moveByInput() before, not after my plugin logic.      moveByInput.call(this);            // My logic    };})();
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
If you're not going to expose the method that you're overriding anyway, why use long an unreadable names for the alias? I never understood.
Because Javascript is camelCase oriented and I like descriptive names. I write a lot of (and I hate doing this) "self documenting code" so the names of things are very important to me. In 6 months I will remember what oldRemberWhatThisFunctionIsCalled but I wont rember old_rwtfic ...
 

Ramiro

Now with an army of Mecha-Ralphs!
Veteran
Joined
Aug 5, 2015
Messages
858
Reaction score
364
First Language
Spanish
Because Javascript is camelCase oriented and I like descriptive names (...) I will remember what oldRemberWhatThisFunctionIsCalled but I wont rember old_rwtfic ...
I don't know man if you read the code above but...

var moveByInput = Game_Player.prototype.moveByInput;
Is both camel case and descriptive...

I usualyl paned the class name because I do just one anonimous function per plugin, so if I alterboth Game_Player and Game_Map's update method, I do:

Game_Map_update and Game_Player_update, or MyClass_update, etc... and I separate them with underscores, because, well, underscores are a valid character, you can use $ if you like:

Game_Map$update, but '$' makes me think in other things thanks to jQuery...
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
If i extend one or more functions of a class, i do this in its own self execution function, which takes the class as a parameter. This has 2 advantages for me:

- I can refer to the class as "$", so i don't have to repeat long class names a dozen times and can easily switch out the class for whatever reason.

- My aliases are named "alias_<function>". Since they are declared in their own scope, i don't have to specify the class name in the alias.

I'm sure it's slower this way, but it's only on startup, so i'm fine with trading a few fractions of a second for an (at least for me) easy to understand code structure.

Example:

Code:
(function() {    "use strict";    // stuff    (function($) {            /**         * During scene update also call our own update function to determine, whether achievements have been completed.         */        var alias_update = $.prototype.update;        $.prototype.update = function() {            alias_update.call(this);            _update();        };        })(Scene_Base);    // more stuff})();
 

ArkDG

Veteran
Veteran
Joined
May 26, 2013
Messages
143
Reaction score
48
First Language
portuguese
Primarily Uses
First, I'm a newbie that started scriptting now with java for rmmv. Tks in advance for the patience and consideration.  :D  

Anyway...

Talking about aliases, I'm using the same way to do it that Sulsay is using. But you were talking now about arguments, and I don't think I understood it well...

Correct me if I'm wrong: The arguments are the "actions" (sorry for the poor language to call these command lines things) that I determine to be done inside the function, and that are separated by ";"?

If yes, can I call just a part of the function and not the wholle function when aliasing it?
How?!

I think this should help me with a problem I'm having with "z index" in a script. 
 

kiriseo

Veteran
Veteran
Joined
Oct 27, 2015
Messages
245
Reaction score
82
First Language
German
But you were talking now about arguments, and I don't think I understood it well...
Correct me if I'm wrong: The arguments are the "actions" (sorry for the poor language to call these command lines things) that I determine to be done inside the function, and that are separated by ";"?

If yes, can I call just a part of the function and not the wholle function when aliasing it?

How?!

I think this should help me with a problem I'm having with "z index" in a script. 
Like lavra stated:

"arguments" is an array-like object, that's available inside a function and contains all parameters passed to it. "apply" takes an array-like object as its second parameter and will pass it to the function as parameters.
arguments are a package of parameters you pass to a function to use them inside of it.

You can't just call a statement ("actions" as you called it  ;) ) of a function and ignore the rest.

When callin' a function, you're using all what's inside.

But you can change the values set by the called function in your alias
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,045
Members
137,569
Latest member
Shtelsky
Top