JavaScript Object-Oriented programming (6) using a prototype chain for inheritance

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.