SoFunction
Updated on 2025-02-28

A good article about javascript-prototype inheritance

1. The most basic usage: Assign an instance of ClassA to ClassB.
ClassB inherits all attributes of ClassA.
The code is entered:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

2. Consider from the perspective of prototype inheritance theory,
The prototype inheritance of js is a reference prototype, not a copy prototype.
Therefore, modifying the prototype will cause changes in all instances of B.
The code is as follows:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

3. However, the write operation of a subclass object only accesses members of the subclass object.
They do not affect each other, so
Writing is writing subclasses Reading is reading prototypes (if there is no subclass).

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

4. Each subclass object holds a reference to the same prototype.
So the prototype members in the subclass object are actually the same.

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

5. When constructing subclasses, the prototype constructor will not be executed

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

6. Next is fatal, accessing the prototype member object in the subclass object:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

7. Therefore, in prototype inheritance, there cannot be member objects in the prototype class! All members must be value type data (string can also be)
Using prototype inheritance has the advantages of high execution efficiency, no memory wasted, and it can be seen immediately in the subclass after dynamically adding methods to the parent class.

Inheritance is inherited by setting the prototype object of the subclass as an instance of the parent class.

Inheritance also has four obvious disadvantages:
Disadvantage 1: The constructor of the parent class is not executed when instantiating the subclass like in JAVA, but when setting inheritance, and is executed only once. This is often not what we want, especially when there are some special operations in the constructor of the parent class.
Disadvantage 2: Since the constructor of the parent class is not executed when the subclass is instantiated, the member variable set in the constructor of the parent class becomes a public variable for all instance objects in the subclass. Since inheritance in JavaScript only occurs when "get" the value of the attribute, the value of the attribute is a type that cannot be modified by the data itself. But there will be problems with Array and Object types.
Disadvantage 3: If the constructor of the parent class requires parameters, we have no choice.
Disadvantage 4: The original prototype object of the subclass is replaced, and the constructor attribute of the subclass itself is gone. When taking its constructor attribute in an instance of a class, the constructor attribute inherited from the parent class is obtained, so that the constructor value is the parent class rather than the child class.

10. Can be modified based on the shortcomings of prototype
For example, it is a method to write it as a Function object, which is convenient for use.

 = function (parentClass)
{
  var Bs = new Function();
   = ;
   = new Bs();
   = parentClass;
   = this;
}

I hope all js experts can introduce you to a better way
For the 3rd and 6th

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

For inheritance,
You cannot inherit ClassA, ClassB

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

Inheritance problem with parameters

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]