Analysis of JavaScript Inheritance Method (1)

Source: Internet
Author: User

Some time ago, I wrote the JavaScript writing method. From this point on, let's look at the Inheritance Method of JavaScript.

Most object-oriented languages Support inheritance. The most important advantage of inheritance is code reuse to build large-scale software systems. If a class can reuse the attributes and methods of another class, it is called inheritance. From this perspective, let's look at the JS inheritance method. The Inheritance Method in JS is closely related to the writing method. Different writing methods lead to different inheritance methods. The inheritance methods for various popular JavaScript libraries are also different. Start with the simplest reuse.

1. Write a class by using the constructor. Copy the attributes/fields of the parent class to the subclass for inheritance.

Here, the parent class and child classes are written in the constructor mode, and no prototype is required. The subclass calls the parent class function to copy the attributes of the parent class.

 
 
  1. /**
  2. * Parent Polygon: Polygon
  3. * @ Param {Object} sides
  4. */
  5. Function Polygon (sides ){
  6. This. sides = sides;
  7. This. setSides = function (s) {this. sides = s ;}
  8. }
  9.  
  10. /**
  11. * Subclass Triangle: Triangle
  12. */
  13. Function Triangle (){
  14. This. tempfun = Polygon; // a property of tempfun assigned to the subclass by the parent class reference
  15. This. tempfun (3); // call
  16. Delete this. tempfun; // delete this attribute
  17. This. getArea = function (){};
  18. }
  19.  
  20. // New objects
  21. Var tri = new Triangle ();
  22. Console. log (tri. sides); // inherited attributes
  23. Console. log (tri. setSides); // The inherited method.
  24. Console. log (tri. getArea); // Method
  25.  
  26. // The disadvantage is that it is false when instanceof is used as the parent class Polygon for the Triangle instance object.
  27. Console. log (tri instanceof Triangle); // true
  28. Console. log (tri instanceof Polygon); // false

Because JavaScript has multiple calling methods for the name function, subclasses can also implement the following methods. The method for calling the parent class in the subclass is different.

 
 
  1. Function Triangle (){
  2. Polygon. call (this, 3); // call the parent class
  3. This. getArea = function (){};
  4. }
  5. Function Triangle (){
  6. Polygon. apply (this, [3]); // use the apply method to call the parent class
  7. This. getArea = function (){};
  8. }
  9. Function Triangle (){
  10. Var temp = new Polygon (3); // The new method calls the parent class
  11. For (recognition in temp) {// copy all to the subclass
  12. This [recognition] = temp [recognition];
  13. }
  14. This. getArea = function (){};
  15. }

The disadvantage of this method is that it is always false when the subclass instance object uses instanceof to check the parent class. This is against the inheritance of "is a" in java.

2. Prototype writing class, prototype inheritance

Core JS's own object system is inherited by prototype based. Or core JS does not use a common class-based system, but uses prototype inheritance to implement its own object system. In our work, we can also use prototype to implement inheritance, and code reuse is used to build our own functional modules.

 
 
  1. /**
  2. * Parent Polygon: Polygon
  3. *
  4. */
  5. Function Polygon (){}
  6. Polygon. prototype. sides = 0;
  7. Polygon. prototype. setSides = function (s) {this. sides = s ;}
  8.  
  9. /**
  10. * Subclass Triangle: Triangle
  11. */
  12. Function Triangle (){}
  13. Triangle. prototype = new Polygon (); // This is the key to prototype inheritance.
  14. Triangle. prototype. getArea = function (){}
  15.  
  16. // New objects
  17. Var tri = new Triangle ();
  18. Console. log (tri. sides); // inherited attributes
  19. Console. log (tri. setSides); // The inherited method.
  20. Console. log (tri. getArea); // self-owned Method
  21.  
  22. // Instanceof Test
  23. Console. log (tri instanceof Triangle); // true, indicating that the object is a Triangle
  24. Console. log (tri instanceof Polygon); // true, indicating that the triangle is also a Polygon

Although the output shows that the subclass inherits the property sides and method setSides of the parent class Polygon, but the sides is 0, how can it be a triangle. You have to call tri. setSides (3) to make it a triangle. This seems inconvenient. Parameters cannot be passed, which is the disadvantage of prototype. The advantage is that the "is a" relationship is correctly maintained.

3. Composite constructor/prototype writing class, inherited using the previous method

In this way, the attributes of the parent class are all attached to the constructor, and the methods are all attached to the prototype.

 
 
  1. /**
  2. * Parent Polygon: Polygon
  3. */
  4. Function Polygon (sides ){
  5. This. sides = sides;
  6. }
  7. Polygon. prototype. setSides = function (s) {this. sides = s ;}
  8.  
  9. /**
  10. * Triangle
  11. * @ Param {Object} base
  12. * @ Param {Object} height
  13. */
  14. Function Triangle (base, height ){
  15. Polygon. call (this, 3); // copy the attributes of the parent class to yourself.
  16. This. base = base;
  17. This. height = height;
  18. }
  19. Triangle. prototype = new Polygon (); // copy the parent class method to yourself.
  20.  
  21. Triangle. prototype. getArea = function () {// finally define your own Method
  22. Return this. base * this. height/2;
  23. }
  24.  
  25. // New objects
  26. Var tri = new Triangle (12, 4 );
  27. Console. log (tri. sides); // inherited attributes
  28. Console. log (tri. setSides); // The inherited method.
  29. Console. log (tri. base); // attributes
  30. Console. log (tri. height); // attributes
  31. Console. log (tri. getArea); // self-owned Method
  32.  
  33. // Instanceof test, indicating that the "is a" relationship is correctly maintained
  34. Console. log (tri instanceof Triangle); // true, indicating that the object is a Triangle
  35. Console. log (tri instanceof Polygon); // true, indicating that the triangle is also a Polygon


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.