Inheritance is one of the object-oriented features, and the primary purpose of inheritance is to reuse. Subclasses can reuse the properties or behaviors of a parent class, which can greatly simplify subclasses and avoid duplication of definitions.
Inherited characteristics 1. Child objects have properties and methods of the parent object
Inheritance feature 2: Sub-object "is a" parent object, with "is-a" characteristics,
If people are animals, then people are the children of animals, embodied in the object, a person must have a reference to the animal instance
The prototype of the subclass points to an instance of the parent class, completing the inheritance, because the instances of the child class have properties and behaviors of the parent class instance.
The Java subclass instance has the Super keyword pointing to the parent class instance, and on inheritance, all object-oriented languages are similar.
Shape function shape () {this.name = ' shape '; this.tostring = function () {return this.name;};} 2d shape function Twodshape () {this.name = ' 2D shape ';} Triangular function Triangle (side, height) {this.name = ' Triangle '; this.side = Side;this.height = Height;this.getarea = Functio N () {return this.side * THIS.HEIGHT/2;};} Inherited characteristics 1. Child objects have properties and methods of the parent object twodshape.prototype = new Shape (); Triangle.prototype = new Twodshape ();//Repair TwoDShape.prototype.constructor = Twodshape; Triangle.prototype.constructor = Triangle;var my = new Triangle (5, ten); Alert (my.tostring ());//No toString method, Inherited alert (my.constructor);/* Inheritance feature 2: Sub-object "is a" parent object, with "is-a" characteristics, such as the human is an animal, then the human is the child of the animal, embodied in the object, a person must have a reference to the animal instance */alert (my instanceof Twodshape);//There is an inherited feature alert (my instanceof Shape);
JavaScript Object-Oriented programming (6) using a prototype chain for inheritance