Answer the question
In order to leave comments, you need to log in
Is it bad to change (augment) the prototypes of built-in JavaScript objects?
I read on JavaScript Garden that changing the prototypes of built-in objects is not good. However, Prototype.js does this, for example.
Is it really that bad to change the prototypes of built-in objects? I'm sawing my library for fun, I wanted my own methods there (like $(element).on(event, action)
in JQuery). In JQuery itself, it seems that these methods are added at the time of the call $()
, but with a change in the prototype it would be easier.
Answer the question
In order to leave comments, you need to log in
The situation is more realistic:
You are writing library A and decide to implement String.ptototype.toURL
Someone else is writing library B and also implements this function.
You include library B in your project and are trying to understand why the function you wrote does not work as intended.
I will answer not popularly and even radically. Extending the prototypes of existing objects is actually the basis of inheritance in the language. Therefore, I believe that from the point of view of the "anatomy" of the language itself, this approach is more than correct.
However, in real life, this creates more problems than it provides solutions. And the technique of "wrapping" objects, ala jQuery, looks safer in general and in particular.
In general, it seems that no one has figured out how to "prepare" prototypal inheritance and what advantages it has. Therefore, js eventually rolled into self-made "constructors", and now classes are in the new standard.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question