Reputation: 10058
I'm currently studying javascript prototype and inheritance, and I have encountered the following paragraphs on MDN
I'm not exactly sure what the author meant by extend Object.prototype or one of the other build-in prototype
. Could someone please clarify the concept, preferably with a code sample? Thanks
Upvotes: 0
Views: 32
Reputation: 664599
The term "built-in prototype" refers to the prototype objects from which standard objects inherit. This includes the language-specified Boolean.prototype
, Number.prototype
, String.prototype
, Symbol.prototype
, Object.prototype
, Array.prototype
, Function.prototype
, Date.prototype
, and the prototype objects for the various Error
s, typed arrays, data structures ((Weak-) Map, Set) and iterators.
It also encompasses other native prototype objects in the environment, for example the DOM (Node.prototype
, Element.prototype
, Document.prototype
, …) and other Web APIs (e.g. XMLHttpRequest.prototype
).
See the definition of built-in objects and the whole section about standard built-in objects in ES6.
In general, you should not mess with them. They are supplied by the environment, they are not yours - don't touch them and create your own methods on them. If you want to write modular, interoperable code, you should not depend on custom, global modifications of built-ins. See also Why is extending native objects a bad practice? for more discussion.
Upvotes: 2
Reputation: 6090
Other things besides Object
, such as Array
and Function
, have prototypes too. It's considered bad practice to extend those prototypes as well, for the reasons mentioned on MDN.
Upvotes: 1