P
P
President422015-03-26 23:55:52
JavaScript
President42, 2015-03-26 23:55:52

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

2 answer(s)
Y
Yuri Puzynya, 2015-03-27
@President42

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.

P
Pavel Malyshev, 2016-08-25
@PaulMaly

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 question

Ask a Question

731 491 924 answers to any question