Prototype chain diagram

The basic prototype chain diagram looks like this, doesn’t it look too convoluted? Here we go

Prototype (prototype)

  • All JS functions have the Prototype attribute, only functions have it
  • Its properties and methods are both accessible to objects instantiated by the constructor

From this example, you can see that the Person method (sayHello) on the prototype chain can be called by the instantiated object P1. When we remove the method from the Person prototype chain, the sayHello method cannot be called on P1Copy the code

The constructor (constructor)

  • constructorExists in every functionprototypeProperty, which points to the function itself

The prototype chain (_proto_)

  • The object in JS will have a_proto_Property that points to the constructor that created itprototype, and functions are special and also have this property

You can see that the _proto_ of p1 instantiated by the Person function points to the prototype of the constructor PersonCopy the code
  • When the JS search engine looks for properties or methods in an object, if there are no such properties or methods on the object, it will look up through the prototype chain layer by layer

From this we can see that p1 does not have the sayHello method but we can find it by looking up the prototype chainCopy the code

Implement a simple prototype chain