Understanding about prototype and inheritance

Source: Internet
Author: User
Tags ibase

Study Notes (enlightened)

Preface

Today, I saw some questions about inheritance and prototype when I was reading <JavaScript advanced programming>. I came back to study it and thanked the JS group netizens for their guidance, thank you very much!

First give demo and then talk
1 function polygon (isides) {2 This. sides = isides; 3 this. sex = "male"; 4 This. getname = function () {5 console. log ("Broken Bridge residual snow 1"); 6}; 7} 8 polygon. prototype. getname = function () {9 console. log ("Broken Bridge residual snow 2"); 10}; 11 polygon. prototype. getarea = function () {12 Return 0; 13}; 14 polygon. prototype. sex = "man"; 15 polygon. age = 100; 16 17 // triangle 18 function triangle (IBASE, iheight) {19 // polygon. call (this, 3); 20 this. base = IBASE; 21 this. height = iheight; 22 This. getname = function () {23 console. log ("Broken Bridge residual snow 4"); 24 }; 25} 26 27 triangle. prototype = new polygon (); 28 triangle. prototype. getarea = function () {29 return 0.5 * This. base * This. height; 30}; 31 triangle. prototype. getname = function () {32 console. log ("wawawa"); 33}; 34 35 36 var OBJ = New Triangle (2, 7); 37 // var area = obj. getarea (); 38 obj. getname (); 39 // console. log (area); 40 console. log (obj. age); 41 console. log (obj. sex );
Summary after testing
  • The new prototype object method can be accessed only by its member instances. The two methods are definitely different (this is nonsense, ), for example:
    1 A.prototype.Method=function(){ };2 A.Method=function(){ };
  • Prototype of prototype can be inherited by transformation based on its particularity.
  • Class Method/object method/prototype method, you must note the following when implementing:
    1 Triangle.prototype = new Polygon();2 Triangle.prototype = Polygon.prototype;

    => The first line is to assign the polygon member instance to the prototype of the triangle, including the object attributes, methods, prototype attributes, and methods of the polygon, excluding class attributes and methods.
    => The second line is to assign the prototype of polygon to the prototype of triangle directly, only including the attributes and methods of the prototype object in polygon.

  • To be accurate, prototype cannot fully implement inheritance, because parameters cannot be passed when protype is used. To implement inheritance, the more appropriate method is to inherit the attributes of the parent class by impersonating an object (prototype can also inherit the attributes of the parent class with limitations ), then use the prototype chain to inherit the parent class method.
  • When the parent class has member method A, and the prototype chain of the parent class also has method A, the subclass inherits the parent class, And the subclass also overwrites the member method A and prototype object method, then their call order priority is: subclass A ()> subclass prototype object method A ()> parent class member method A ()> parent class prototype object method ()
Conclusion

Well, today something went on, delayed, and almost forgot. Otherwise, I will see you in the face of my face tomorrow. To tell the truth, I will know this when I come back to study, I hope you will see any errors or deficiencies. Thank you!

Today, as a programmer's team, I grabbed a train ticket on the 11th and used a browser plug-in on the client app. I still had to grab the ticket ......

Understanding about prototype and inheritance

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.