简体   繁体   English

对短语“扩展Object.prototype或其他内置原型之一”的混淆

[英]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 我目前正在研究javascript原型和继承,并且在MDN上遇到了以下段落

I'm not exactly sure what the author meant by extend Object.prototype or one of the other build-in prototype . 我不确定作者extend Object.prototype or one of the other build-in prototype含义。 Could someone please clarify the concept, preferably with a code sample? 有人可以用一个代码示例来澄清这个概念吗? Thanks 谢谢

在此处输入图片说明

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. 这包括语言指定的Boolean.prototypeNumber.prototypeString.prototypeSymbol.prototypeObject.prototypeArray.prototypeFunction.prototypeDate.prototype以及各种Error的原型对象(类型化数组) ,数据结构((弱)映射,集合)和迭代器。

It also encompasses other native prototype objects in the environment, for example the DOM ( Node.prototype , Element.prototype , Document.prototype , …) and other Web APIs (eg XMLHttpRequest.prototype ). 它还包含环境中的其他本机原型对象,例如DOM( Node.prototypeElement.prototypeDocument.prototype …)和其他Web API(例如XMLHttpRequest.prototype )。

See the definition of built-in objects and the whole section about standard built-in objects in ES6. 请参阅内置对象定义以及有关 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. 进行更多讨论。

Other things besides Object , such as Array and Function , have prototypes too. 除了Object以外的其他东西,例如ArrayFunction ,也都有原型。 It's considered bad practice to extend those prototypes as well, for the reasons mentioned on MDN. 由于MDN中提到的原因,扩展这些原型也被认为是不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM