Context:
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 (
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:
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.
Instead of
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?
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?
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)
JavaScript:
Array.prototype.shuffle = function() { ... }
a = [1,2,3,4,5,6]
a.shuffle()
Thought?