Search code examples
javascriptprototypeencapsulation

confusion over the phrase "extend Object.prototype or one of the other build-in prototype"


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

enter image description here


Solution

  • 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 Errors, 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.