In basic usage, assigning an instance of classa to classb inherits all the attributes of classa.
< Script >
Function Classa ()
{
This.='A ';
}
Function Classb ()
{
This. B='B ';
}
Classb. Prototype = New Classa ();
VaR Objb = New Classb ();
For ( VaR P In Objb) document. Write (P + " <Br> " );
</ Script >
From the perspective of prototype Inheritance theory, the prototype inheritance of JS is to reference prototype, not to copy prototype.
Therefore, modifying the prototype will change all B's instances.
< Script >
Function Classa ()
{
This.='A ';
}
Function Classb ()
{
This. B='B ';
}
Classb. Prototype = New Classa ();
VaR Objb = New Classb ();
Alert (objb. );
Classb. Prototype. = 'Changed !! ';
Alert (objb. );
</ Script >
However, the write operation of subclass objects only accesses the members in the subclass objects and they do not affect each other.
Therefore, write is a subclass. Read is a read prototype (if not in the subclass)
< Script >
Function Classa ()
{
This.='A ';
}
Function Classb ()
{
This. B='B ';
}
Classb. Prototype = New Classa ();
VaR Objb1 = New Classb ();
VaR Objb2 = New Classb ();
Objb1.a = ' !!! ';
Alert (objb1.a );
Alert (objb2.a );
</ Script >
Each subclass object has reference to the same prototype. Therefore, the prototype members in the subclass object are actually the same
< Script >
Function Classa ()
{
This.=Function(){Alert ();};
}
Function Classb ()
{
This. B=Function(){Alert ();};
}
Classb. Prototype = New Classa ();
VaR Objb1 = New Classb ();
VaR Objb2 = New Classb ();
Alert (objb1.a = Objb2.a );
Alert (objb1. B = Objb2. B );
</ Script >
The prototype constructor is not executed when the subclass is constructed. < Script >
Function Classa ()
{
Alert ("A");
This.=Function(){Alert ();};
}
Function Classb ()
{
Alert ("B");
This. B=Function(){Alert ();};
}
Classb. Prototype = New Classa ();
VaR Objb1 = New Classb ();
VaR Objb2 = New Classb ();
</ Script >
The next step is fatal. Access the prototype member object in the subclass object: < Script >
Function Classa ()
{
This.=[];
}
Function Classb ()
{
This. B=Function(){Alert ();};
}
Classb. Prototype = New Classa ();
VaR Objb1 = New Classb ();
VaR Objb2 = New Classb ();
Objb1.a. Push ( 1 , 2 , 3 );
Alert (objb2.a );
// All a members in all B's instances have changed !!
</ Script >
Therefore, prototype inheritance does not contain 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 waste of memory, dynamic addition of methods for the parent class, and so on in the subclass.
I like prototype inheritance very much.
Prototype inheritance is inherited by setting prototype of the subclass to an instance of the parent class.
Simply setting inheritance in this way does have many disadvantages, as the landlord said. In general, there are four disadvantages:
Disadvantage 1: The constructor of the parent class is not executed when the subclass is instantiated as in Java, but executed only once when the inheritance is set. This is often not what we want, especially when the constructor of the parent class has some special operations.
Disadvantage 2: The constructor of the parent class is not executed when the child class is instantiated, the member variables set in the constructor of the parent class become the public variables of all instance objects when they are in the subclass. Because the inheritance in Javascript only occurs when the value of the "get" attribute is "string", the data of the attribute value "string", "Number", and "Boolean" cannot be modified. However, the array and object types are problematic.
Disadvantage 3: If the constructor of the parent class requires a parameter, there is no way.
Disadvantage 4: The original prototype object of the subclass is replaced, and the constructor attribute of the subclass is no longer available. When an instance of the class obtains its constructor attribute, it obtains the constructor attribute inherited from the parent class, so that the constructor value is the parent class rather than the Child class.
I once had a headache for these four shortcomings, So I transformed prototype inheritance.
I tried several methods. below is the best one I think. I write it as a function object method, which is convenient to use. The method is as follows: // Class inheritance-wave Edition
Function. Prototype. extends = Function (Parentclass)
{
VaR BS = New Function ();
BS. Prototype = Parentclass. Prototype;
This . Prototype = New BS ();
This . Prototype. Super = Parentclass;
This . Prototype. Constructor = This ;
}