欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Prototypes

程序员文章站 2022-07-06 19:56:39
...

Every JavaScript object has a second JavaScript object (or null, but this is rare) associate with it. This second object is known as a prototype, and the first object inherits properties from the prototype.

 

All objects created by object literals have the same prototype object, and we can refer to this prototype object in JavaScript code as Object.prototype.

 

Objects created using the new keyword and a constructor invocation use the value of the prototype property of the constructor function as their prototype.

 

So the object created by new Object() inherits from Object.prototype just as the object created by {} does.

 

Object.prototype is one of the rare objects that has no prototype: it does not inherit any properties.

 

Other prototype objects are normal objects that do have a prototype.

 

All of the built-in constructors(and most user-defined constructors) have a prototype that inherits from Object.prototype. For example,Date.prototype inherits properties from Object.prototypes,

so a Date object created by new Date() inherits properties from both Date.prototype and Object.prototype. This linked series of prototype objects is known as a prototype chain.