You did n’t know about the JS series (50)-those deceptive terms in JS

In JS, we do not copy an object (class) to another object (instance), but just associate them. The [[Prototype]] mechanism is shown below
 

 

 


This mechanism is often referred to as prototypal inheritance. This name is mainly intended to correspond to the meaning of inheritance in a class-oriented world, but it overturns the corresponding semantics in dynamic scripts.

Inheriting the word will give people very strong psychological expectations. Just adding a prototype in front cannot distinguish the almost completely opposite behavior of class inheritance in js

So I think this confusing combination term "prototype inheritance". And other class-oriented terms such as class, constructor, instance, polymorphism, etc. have seriously affected everyone's understanding of the true principle of js mechanism

Inheritance means copy operation, JS does not copy object properties. Instead, JS creates an association between two objects so that one object can access the properties and functions of another object through delegation.

There is also a term differential inheritance that is occasionally used. The basic principle is to use the characteristics that are different from the general description when describing the behavior of the object. For example, when describing a car, you would say that the car is a vehicle with four wheels, but you will not repeat the general characteristics of the vehicle, such as the engine

By default, objects are not generated by copying as implied by differential inheritance. Therefore, differential inheritance is also not suitable for describing the [[Prototype]] mechanism of JS

Guess you like

Origin www.cnblogs.com/wzndkj/p/12689154.html
Recommended