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.