Share the way you Alias functions

This is my current method:

(function($){ var aliased_initialize = Window_Base.prototype.initialize; $.prototype.initialize = function(x, y, width, height){ aliased_initialize.call(this,x,y,width,height); //new stuff };})(Window_Base);Not sure which way is best.

Does encapsulation even matter(If we have unique names for everything)?
Just imagine 50 plugins, that clutter the global window object and make it very hard to actually debug that mess. Additionally, a variable added to the public scope will NEVER be garbage collected and exists for the lifetime of the game, so it's very easy to unintentionally cause memory leaks.


Also, hiding your private variables from other scripts/plugins is a fundamental part of objected-oriented programming and even though JavaScript doesn't support traditional classes (ES6 classes are merely syntactic sugar), the fundamentals stay the same.
How many times do you see scripters use variables like _variable? Those are obviously intended to be 'private'. Yet nobody I ever saw bothers to do it 'right' when it comes to encapsulation. Obviously you should add a (getter-)method to return private variables. Or _characterSprites from Spriteset_Map. Most people would just use the _characterSprites variable directly without adding a property-getter to the Spriteset_Map. Most classes in RPG Maker have such getters build-in but not always. And when this is not the case, everyone just ignores encapsulation anyway. Or the amount of people (including me) who doesn't bother to use underscores at all for their own code.

Example from Yanfly:

$gameSystem._showLearnSkill = true; // Goodbye encapsulation!Note the underscore. Really, encapsulation in Javascript is ignored even by Yanfly, all over the place. And if someone else wants to make a patch for my plugin and runs into errors because they use private-stuff that he isn't supposed to change, well, not my problem. With an anonymous function that wouldn't even be possible anyway.

And memory leaks aren't created that easily. Unless the coder is a total beginner, those people should use anonymous functions just to catch their mistakes ye. But those mistakes should eventually be fixed instead of relying on an anonymous function anyway because they could cause other side-effects.

And the only global variables I use (usually Imported & Silv) are intended to be shared with other plugins anyway. The primary reason why I make them global. The secondary reason is that it's actually EASIER do debug, not harder. All my important data is all stored in a single global variable that I can access from anywhere, including the console, in an instant:



Costs a little bit of extra RAM (probably not even 10kb) but that's it. Fully intended, no leaks. W/o using this global variable debugging would be hard because a lot of stuff would be gone after it would finish processing. This makes it easier. Just don't store images or something crazy in it. Also doesn't clutter the window object because it's just 1 variable for a ton of scripts. And many more reasons.
So, in short: Other people do it the wrong way, so that's fine for me, too? If you need to add something to $gameSystem, it's best to add your own namespace to it, like this:

$gameSystem.IAVRA.myVariable;A class can refer to its own variables via _myVariable just fine, but for outside use, you should define a property, like this:
Code:
Object.defineProperty(MyClass.prototype, 'myVariable', {    get: function() { return this._myVariable; },     set: function(value) { this._myVariable = value; }});
This also allows you to do validation or conversation, if needed and ensures the integrity of data you receive.What you linked is btw NOT the only global variable you declare. If you are not wrapping your plugin in an anonymous function, everything you declare with "var" gets leaked into the public scope. Even worse, because the base scripts don't use strict mode, you don't, too, and you can unintentionally declare public variables inside functions if you forget the "var", your eval()s can modify the public scope etc etc.
$gameSystem.IAVRA.myVariable;
Then yes then you would respect the encapsulation. But if 50 plugins from 50 different coders would do this, you would end up with 50 getter-methods... And maybe another 50 setters. Imo that is much more messy than just directly retrieving the private variable as long as you know that you can retrieve it safely that way w/o causing side-effects. Imo this is anothe design flaw from the RM-crew. Other plugins may break that at some point but again, not my problem 😛 .

Yes but Yanfly in that example did it for outside use, I do it too and every other plugin I saw does it too (unless it just so happens to have no need for any such variable of course).

What you linked is btw NOT the only global variable you declare. If you are not wrapping your plugin in an anonymous function, everything you declare with "var" gets leaked into the public scope. Even worse, because the base scripts don't use strict mode, you don't, too, and you can unintentionally declare public variables inside functions if you forget the "var", your eval()s can modify the public scope etc etc.
Ugh... Well... Javascript just doesn't support encapsulation and the RPG Maker code lacks some getters/setters here and there, making me believe they don't want us to encapsulate properly. On top of that the strict-mode was indeed another issue. Aside from the fact that it possibly bugs in RM, they indeed don't use it in the base-scripts anyway.

All by all the cons by far outweigh the pro's for me. And thus I'm dumping the anonymous function.

Aliasing works nice, better than in Ruby imo. But you gotta make sure your names are unique.
Ugh... Well... Javascript just doesn't support encapsulation 

Uh ....

(function(){})is a form of encapsulation .... 
Uh ....

(function(){})is a form of encapsulation ....
Javascript does not support it. Great we found a hack (that is what it is) to get it to work (to some degree) but Javascript itself does not support it.

Also you'd have to add extra anonymous functions inside your anonymous function to encapsulate your private members within your own code as well. You can probably imagine that this is not exactly gonna look pretty. The more complicated scripts would be filled and filled with anonymous functions all over the place, making it insanely hard to read the code.
I don't find it that hard to read actually. It makes nice little blocks of code.
Um 

Javascript does not support it. Great we found a hack (that is what it is) to get it to work (to some degree) but Javascript itself does not support it.

Also you'd have to add extra anonymous functions inside your anonymous function to encapsulate your private members within your own code as well. You can probably imagine that this is not exactly gonna look pretty. The more complicated scripts would be filled and filled with anonymous functions all over the place, making it insanely hard to read the code.
With ES6, yes I know not every one is like me, but with ES6 we have more of a true concept of classes and classes are a form of encapsulation. Because ES6 classes are a thin wrapped on prototypes, you could argue that prototypes are in them selves a form of encapsulation.

The true "meaning" of encapsulation is to insulate the functionality or variables from the out side world. Both anonymous functions and prototypes do this, to some degree. 

As long as you can wrap some data or functionality in some kind of object you basically have a sense of encapsulation.

Original thread: https://forums.rpgmakerweb.com/threads/share-the-way-you-alias-functions.52058/