Javascript-class inheritance

Source: Internet
Author: User
//  The method of class inheritance is packaged with extedn      Function  Extend (subs, sups ){  //  Change the prototype object of the subclass to the Instance Object of the parent class. In this way, all the public methods of the parent class can be found through the prototype chain principle and the Instance Object of the subclass. Subs. Prototype = New  Sups ();  //  The constructor attribute of the subclass points to the constructor of the subclass. Subs. Prototype. constructor = Subs;  //  The supperclass attribute of the subclass points to the parent's prototype object. Subs. supperclass =Sups. Prototype;  /*  If the prototype object of the parent class is changed to an object created by itself, the constructor of the parent class will not point to the parent class constructor. * reader. supperclass. constructor. the call (this, name) statement does not execute the constructor of the parent class, so it cannot inherit the name attribute of the parent class. * therefore, we need to make a judgment here.  */          If (Sups. Prototype. constructor = Object. Prototype. constructor)  //  Direct the constructor attribute of the parent class to the constructor of the parent class. Sups. Prototype. constructor = Sups ;}  //  Parent class      Function Person (name ){  This . Name = Name ;}  //  Public method of parent class Person. Prototype. getname = {Getname:  Function  (){  Return   This  . Name ;}}  //  Subclass      Function Reader (like, name ){  /*  === Person. call (this, name) ==== should have been written in this way, but in order not to let the parent class name appear in the * function of the subclass, that is, to decouple, so the following method is used, the level subclass adds a supperclass attribute, and then  */  Reader. supperclass. constructor. Call (  This  , Name );  This . Like = Like ;}  /*  Execute inheritance. Only after this execution can you add the public method of the subclass to the prototype object of the subclass. Otherwise, the public method added to the prototype object of the subclass is overwritten because of the extend (reader, person); (the overwriting is inaccurate, but I don't know any other words .)  */ Extend (reader, person );  //  Public method of subclass Reader. Prototype. getlike = Function  (){  Return   This  . Like ;}  //  Instantiate subclass      VaR Reader01 = New Reader ('books ', 'zj' ); Console. Log (reader01.getlike (); console. Log (reader01.getname ()); 

 

The key to 'classically inherited 'I understand is:

1. in the constructor of the subclass, call the constructor of the parent class (use the call method to direct the call scope to the instantiated object). In this way, when the subclass instantiates the object, you can add attributes of the parent class to this object.

 

2. Change prototype of the subclass to the instantiated object of the parent class, so that the instantiated object of the subclass can inherit the public methods in the parent class through the prototype link.

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.