I know this was a contentious issue five years ago, but I’m wondering if things have changed for today’s JavaScript. Are there any real world examples of a major modern library being incompatible with extending Object.prototype?
I’m not interested in hypothetical “someone may write bad for in iteration code in a library that you want to use, maybe, in the future, and then you might get a weird bug”
Yes, I remember problems with jQuery, -which is one of the less intrusive libraries- for example:
Another case that I remember is that someone added a
loadfunction to theObject.prototypeobject, and it caused problems with the$().loadevent:Example here.
Augmenting the
Object.prototypeobject in that way is never recommended, because those properties will be inherited by a great number of objects -even also by some host objects-, and as you know, the primary concern is that they will be enumerated by thefor-instatement.In ECMAScript 5, now a safer way exist, because we can now declare non-enumerable properties, for example:
In the property descriptor –
{ value: 'bar' }– we can specify property attributes, in the case of Value Properties as in the above example, we can specify thewritableattribute, and the commonconfigurableattribute (determines if a property can be re-configured -attribute changes- or deleted.And we have also the
enumerableattribute, which determines if the property will be enumerated by thefor-instatement.If we don’t specify the attributes, they are
falseby default, the descriptor will look like: