So I basically did what the title suggests not to do and I had to learn my lesson due to an incompatibility with someone else's plugin.
Now I'd like to share my newly discovered expirience with you. This can be seen as advice or even as a discussion topic.
I guess pro javascript developers on this forum were already well aware of this and wouldn't have done it anyway, but maybe there are other noobs like me who think it might be convenient and actually didn't know you shouldn't extend standard types like Array.prototype as doing so might result in really strange problems and compatibility issues.
Now that I know that, I've found that even the Javascript documentation advises against it and multiple discussion on stackoverflow marking this as a bad practise.
To give an example what I mean, let's use Array.prototype, the type that I tried to extend:
If you save this as a plugin in your plugin folder and activate it, you'll get the following console output:
So each of the three loops logs the same.
Now let's add a method to Array.prototype with the usual syntax:
On stackoverflow some people suggest however you could basically extend standard types like this, at least without running into the for..in loop problem:
Which results in the original console output.
But there are also people claiming even this as bad practise causing compatibility issues with others peoples code.
The general answer for not doing this either seems to be naming conflicts and unexpected behaviour if someone overrides your property, even though this can be avoided by leaving configurable = false.
Well actually not. If someone will not use properties, but overwrite Array.prototype.test with something else, like a function again, this will work, even tough configurable = false.
I think this might be an interesting discussion topic with pro javascript users. Would you also generally discourage people like me from adding stuff they think might be convenient to standard types in their publicly released plugins via properties?
Now I'd like to share my newly discovered expirience with you. This can be seen as advice or even as a discussion topic.
I guess pro javascript developers on this forum were already well aware of this and wouldn't have done it anyway, but maybe there are other noobs like me who think it might be convenient and actually didn't know you shouldn't extend standard types like Array.prototype as doing so might result in really strange problems and compatibility issues.
Now that I know that, I've found that even the Javascript documentation advises against it and multiple discussion on stackoverflow marking this as a bad practise.
To give an example what I mean, let's use Array.prototype, the type that I tried to extend:
JavaScript:
var x = [2,3,5];
for( let q in x )
{
console.log(x[q]);
}
for( let q of x )
{
console.log(q);
}
x.forEach(q => console.log(q));
So each of the three loops logs the same.
Now let's add a method to Array.prototype with the usual syntax:
JavaScript:
var x = [2,3,5];
Array.prototype.test = function() {
console.log("test");
}
for( let q in x )
{
console.log(x[q]);
}
for( let q of x )
{
console.log(q);
}
x.forEach(q => console.log(q));
Array.prototype.test is a new function and surprise, surprise it is enumerable and therefore iterated over in the for..in loop, which is basically what I didn't know and also unknowlingly avoided by not using for..in loops over arrays. So general advice: Please don't use for..in loop for arrays either, it is meant for associative containers, but not for indexed arrays.
Use for..of loop or forEach function!
And to show you, what the problems was I had to deal with. The other plugin creator tried to iterate over an array of JSON-Formatted strings and parse them:
and my array extensions where causing the same parse error right at the start of the game, making it unable to start.
Use for..of loop or forEach function!
And to show you, what the problems was I had to deal with. The other plugin creator tried to iterate over an array of JSON-Formatted strings and parse them:
and my array extensions where causing the same parse error right at the start of the game, making it unable to start.
On stackoverflow some people suggest however you could basically extend standard types like this, at least without running into the for..in loop problem:
JavaScript:
Object.defineProperty(Array.prototype, "test", {
value: function() {
console.log("test");
},
configurable: false, // This is the default so not actually required
enumerable: false // This is the default
});
But there are also people claiming even this as bad practise causing compatibility issues with others peoples code.
The general answer for not doing this either seems to be naming conflicts and unexpected behaviour if someone overrides your property, even though this can be avoided by leaving configurable = false.
Well actually not. If someone will not use properties, but overwrite Array.prototype.test with something else, like a function again, this will work, even tough configurable = false.
I think this might be an interesting discussion topic with pro javascript users. Would you also generally discourage people like me from adding stuff they think might be convenient to standard types in their publicly released plugins via properties?