Inheritance, in JS through the prototype chain implementation. Such as:
function box () { this. Name= "Lee";} function Desk () { this. age=100;} // inheriting from the prototype chain, the object of the superclass instantiation is assigned to the prototype of the subclass desk.prototype=New Box (); var desk=New desk (); alert (desk.name);
In this way, desk's prototype gets the instance object + object information of box and forms the prototype chain.
Inheritance follows the nearest principle, and subclasses inherit the attributes closest to the parent class. Such as:
function box () { this. Name= "Lee";} // Add prototype property name Box.prototype.name= "Jack"; function Desk () { this.age=100;} Desk.prototype=new Box (); var desk=New desk (); alert (desk.name); If there is one in the instance, then go back to the prototype chain.
Subtype from which it belongs to itself or its super-type
Alert (desk instanceof desk);
Alert (desk instanceof Box);
On the use of the combination of object impersonation and prototype chain inheritance, to solve the construction method of communication and prototype sharing problems, later to further study.
JavaScript inheritance (temporarily omitting brokered functions, combined inheritance, and parasitic inheritance)