** Why there are prototypes and prototype chains Let’s start with an example function Person(name, Age) {this.name = name this.age = age this.eat = function() {console.log(${age} age ${name} is eating)}}

let person1 = new Person(‘aki’, 19) let person2 = new Person(‘aki’, 19)

Console. log(person1 === person2) // false Copy code why print false? For each instance of the same function, we create a new heap and add attributes to each instance. So in the above function, the two objects are different. It’s easy to run out of memory if a new heap is created for each object, and stereotype chaining is designed to solve this problem because properties and methods defined using the stereotype pattern are shared by all instances, so instances access the same properties and functions. Instead of adding properties to each instance object, we add properties to the constructor’s prototype object, so that the instance object can dynamically retrieve the constructor’s properties and methods just by finding the prototype through the prototype chain. If the above paragraph is not easy to understand, I will use examples to clarify the concepts and their relationships. Function Person() {function Person() {function Person() {

} let person1 = new Person() person1.name = ‘aki’ console.log(person1) Makes sense, right? Prototype First let’s talk about Prototype. In the book JavaScript Advanced Programs:

Whenever a function is created, a Prototype attribute (pointing to the prototype object) is created for the function according to specific rules.

So the Person constructor also has the Prototype property pointing to the instance prototype, as shown below.

So what is a prototype? A stereotype is an object whose properties an instance “inherits.” Properties defined on the stereotype are also owned by the instance through inheritance. The “inheritance” behavior is implemented inside the new operator. Proto An implicit __proto__ attribute that every JavaScript object (except null) has. The proto attribute points to the object’s prototype. You can prove this with code: function Person() {function Person() {

} let person = new person () console.log(person.proto === person.prototype) //true

The constructor and instance object have a Prototype and proto attribute, respectively, pointing to the instance object’s prototype. Does the stereotype have attributes that point to a constructor (function) or to an instance object (object)? The answer is yes, but the prototype only points to properties of the constructor, after all, a constructor can generate many instance objects. This property is constructor. Function Person() {function Person() {

} let person = new person () the console. The log (person) prototype) constructor = = = person) / / true copy code To complement of above, we can get:

Note that constructor is an attribute of the stereotype; constructor refers to the actual constructor. Don’t confuse the two names

Understanding Stereotypes So far, we have seen the relationships between constructors, instance objects, and instance stereotypes:

Each constructor has a prototype object, which contains a pointer to the constructor, and each instance contains an internal pointer to the prototype object, _proto_

More talk is useless, on the code: /*

  • Constructors can be function expressions or function declarations.
  • So you can do either of the following
  • function Person() {}
  • let Person = function() {}
  • * /

function Person() {}

/ *

  • Once declared, the constructor has a stereotype object associated with it
  • * /

console.log(typeof Person.prototype) console.log(Person.prototype)

/ *

  • The stereotype object has a constructor property that references the constructor
  • * /

console.log(Person.prototype.constructor === Person)

/ *

  • A normal prototype chain terminates at the prototype Object of Object
  • The prototype of the Object prototype is NULL
  • * /

console.log(Person.prototype.proto === Object.prototype) console.log(Person.prototype.proto.constructor === Object) console.log(Person.prototype.proto.proto === null)

/ *

  • Two instances created by the same constructor
  • Share the same prototype object
  • * /

let person1 = new Person(), person2 = new Person() console.log(person1.proto === person2.proto)

/ *

  • Instanceof can check if the stereotype chain of an instance contains a stereotype for the specified constructor
  • * /

console.log(person1 instanceof Person) console.log(person1 instanceof Object) console.log(person1.prototype instanceof Object)

—–Python Object-oriented Programming (full version) **** B station BV1Cg411G75N