What is a prototype

Every reference object has an implicit prototype __proto__, and this property is an object. The implicit prototype’s __proto__ refers to its constructor, prototype, the explicit prototype. When you try to get a property of an object that doesn’t have the property itself, look in its implicit __proto__ (prototype) constructor.

Function Person(name) {this.name = name} let p = new Person('xl') p.__proto__ === person.prototype // true, There is actually a step in the new operator to make the new empty object Prototype point to the constructor's Prototype, so you can access the property console.log(p) console.log(person.prototype) on the Prototype chain.Copy the code

You can see that the constructor prototype object has a constructor property pointing to the constructor, so we have another closed loop,Person.prototype.constructor === Person // true

Prototype chain

A stereotype object may also have stereotypes from which it inherits methods and properties, layer by layer, and so on. This relationship, often referred to as the prototype chain, explains why one object has properties and methods defined in other objects. All Object prototypes will eventually point to object. prototype, and the Object prototype will point to null in order to avoid infinite loops

  • Each object __proto__ refers to the Prototype of its constructor
p.__proto__ === Person.prototype
Copy the code
  • The __proto__ constructor refers to the Object’s prototype
Person.prototype.__proto__ === Object.prototype
Copy the code
  • All constructors are function objects, so Object’s __proto__ refers to the function’s Prototype
Object.__proto__ === Function.prototype
Copy the code
  • The prototype Object of the function is also an Object, so it points to the prototype of Object
Function.prototype.__proto__ === Object.prototype
Copy the code
  • The __proto__ of the Object prototype eventually points to null, ending the process of the prototype chain
Object.prototype.__proto__ === null
Copy the code

conclusion

Write not good, some mistakes or did not express clear place also please all big guy to correct, thank the family Thanksgiving 🙏