MZ Common names conflict. Adding functions to the (built-in JS default) prototypes

Context:
FD2BjCHVQAArul9


I'm mainly posting this to raise awareness. Some of you might want to add helper functions to the built-in JS prototypes. Heck, or even the RM prototypes (Game_Battler.prototype, Game_Player.prototype, etc). But while it is nice to have a function so that you can do something like [1,2,3,4,5,6].shuffle(), there is a chance that other plugin devs have the same thought.

In this particular case, we're talking about the shuffle function for Array. While the goal remains the same (shuffle the array content), different dev probably thought of a different thing. For example:
  • Should the function return undefined? (i.e, it modified the contents of the array but the function doesn't return anything)
  • Should the function return the modified array itself? (i.e, you can do something like arr.shuffle().shift())
  • Should the function return a modified copy of the array?
To be fair, while writing the code I totally expect this conflict could actually happen. Although I was optimistic that it probably wouldn't break. It totally would.

Fortunately, to avoid such a thing, it is not that hard. Although it is probably not as pretty as it could be. For example, if I were going to make a shuffle function to an array. I could do it this way.
JavaScript:
shuffleArr = function() { ... }
a = [1,2,3,4,5,6]
shuffleArr.call(a)
Instead of
JavaScript:
Array.prototype.shuffle = function() { ... }
a = [1,2,3,4,5,6]
a.shuffle()
This is probably why core engines have so many compatibility problems as they add (or modify) too many common function/variable names to the prototypes (this is the important part because you can avoid it if you are making an IIFE scoped function than attaching it to the prototype).

Thought?
Actually a neat idea. It's true that most people in this community add/modify functions directly in the prototype. Personally, to avoid those kind of troubles, I just add something at the beginning of the function so it's less likely to have any conflict. Like
Code:
Game_Battler.prototype.mush_paramAbility

Sometimes I also just add random numbers at the end, like:
Code:
Game_Battler.prototype.berserkMode_213
I just type random numbers at the end. Since when you type, most IDE will fill in the rest of the function before you finish typing, it doesn't cause any trouble when coding.

Of course, I only do that for plugins I share. Most of my plugins are for personal usage so I just don't bother doing any of that.
Some options:
  1. A separate function on the plugin namespace, e.g.

    $.shuffle(array)
    I tend to use this for internal functions.

  2. A new property with a user-configurable identifier, e.g.

    array[PluginManager.parameters['function name: shuffle']]
    I tend to use this mostly for user-facing methods/properties (script call stuff).

  3. A new property with a symbolic identifier (ES6+), e.g.

    array[$.SYM.shuffle]()
    Good for instanced properties! More info:
The symbol identifier seems interesting. I might want to explore more on it.
Another suggestion, but I'm not sure if it's halal- I've been adding an if clause before appending default prototypes with my own functions.

ecils.png
@subengari it is not a good idea, because if the other plugins already defined the function and it didn't behave the way you wanted, your plugin is gonna break.

Original thread: https://forums.rpgmakerweb.com/threads/common-names-conflict-adding-functions-to-the-built-in-js-default-prototypes.142018/