The key is to set the prototype attribute of classb to the instance of classa.
Function Classb (color ){ This . Color = Color; This . Saycolor = Function () {Console. Log ( This . Color );}} Function Classa (name ){ This . Name = Name; This . Sayname = Function () {Console. Log ( This . Name )}}
// This sentence allows a to inherit all the attributes and methods of B. classa. Prototype = New Classb (); VaR OA = New Classa (); Oa. Color = "Red" ; Oa. saycolor ();
The relationship is as follows:
Instance a can inherit all the attributes and methods in the classa. Prototype object.
Now classa. Prototype points to the classb instance
Therefore, instance a can have the attributes of all methods of the classb instance.
Classb instances can have all the attributes and methods of classb. prototype,
Therefore, instance a can also have all the attributes and methods of classb. prototype.
It is connected like a chain, so it is called a prototype chain.
Originally:
After classa. Prototype = new classb ();