Use the prototype attribute to simulate class inheritance

Source: Internet
Author: User

Use the prototype attribute to simulate class inheritance: first define a Circle class as the parent class, and then define the subclass
PositionCircle.

Js Code  
  1. Function Circle (radius) {// defines the parent class Circle
  2. This. radius = radius;
  3. }
  4. Circle. prototype. area = function () {// defines the method of the parent class. area: calculates the area.
  5. Return this. radius * this. radius * 3.14;
  6. }
  7. Function PositionCircle (x, y, radius) {// defines the class PositionCircle
  8. This. x = x; // The x coordinate of the attribute.
  9. This. y = y; // attribute ordinate
  10. Circle. call (this, radius); // call the method of the parent class, which is equivalent to calling this. Circle (radius) and setting
  11. // Radius attributes
  12. }
  13. PositionCircle. prototype = new Circle (); // you can specify the parent class of PositionCircle as the Circle class.
  14. Var pc = new PositionCircle (1, 2, 1 );
  15. Alert (pc. area (); // 3.14
  16. // The Area Method of the positioncircle class inherits from the circle class, while the area method of the circle class
  17. // The Area Method inherits the prototype object corresponding to its prototype attribute.
  18. Alert (PC. radius); // The radius attribute of the positioncircle class inherits from the circle class.
  19. /*
  20. Note: Previously we set the prototype attribute of the positioncircle class to point to a circle object,
  21. Therefore, the prototype attribute of PC inherits the prototype attribute of the circle object, while the constructor of the circle object belongs
  22. (That is, the constructor attribute of the prototype object corresponding to the Circle object) is directed to the circle.
  23. Is CIRC.
  24. */
  25. Alert (PC. constructor); // circle
  26. /* For this reason, after we have designed the inheritance relationship of the class, we also need to set the constructor attribute of the subclass. Otherwise, it will point to the parent class.
  27. Constructor attribute
  28. */
  29. PositionCircle. prototype. constructor = PositionCircle
  30. Alert (pc. constructor); // PositionCircle

 

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.