JavaScript Object-oriented (3)--prototype and inheritance

Source: Internet
Author: User

1. Using prototype to implement inheritance  

1 functionBaseClass ()2 {3     varPrivatename = "Private";4     5      This. pubicName1 = "Public1";6     7     varPrivatemethod =function()8     {9Alert ("Privatemethod");Ten     } One      A      This. publicMethod1 =function() -     { -Alert ("PublicMethod1"); the     } -      -      This. Alertprivatename =function() -     { + alert (privatename); - Privatemethod (); +     } A      at } -  -BaseClass.prototype.publicName2 = "Public2"; -BASECLASS.PROTOTYPE.PUBLICMETHOD2 =function() - { -Alert ("PublicMethod2"); in } -  toBaseclass.staticname = "Static"; +Baseclass.staticmethod =function() - { theAlert ("Staticmethod"); * } $ Panax NotoginsengMyClass =function() - { the      This. GetName =function() +     { A         //cannot access the private properties and methods of the base class the alert (privatename); + Privatemethod (); -     } $ } $  -Myclass.prototype =NewBaseClass (); -Object =NewMyClass (); the  - //access to base class public propertiesWuyi alert (object.pubicname1); the alert (object.publicname2); - //can access base class static properties Wu alert (object.constructor.staticName); - //access to the base class public method About object.publicmethod1 (); $ object.publicmethod2 (); - //access to base class static methods - Object.constructor.staticMethod (); - //private properties and methods of the base class can be accessed through the base class public method AObject.alertprivatename ();

Myclass.prototype = new BaseClass () implements the inheritance of MyClass to BaseClass. MyClass can access the public properties of the base class, static properties, public methods, static methods, and the private properties and methods of the base class indirectly through the public methods of the base class. Very perfect inheritance, isn't it!

2. Using me to implement inheritance

functionBaseClass () {varPrivatename = "Private";  This. pubicName1 = "Public1"; varPrivatemethod =function() {alert ("Privatemethod"); }         This. publicMethod1 =function() {alert ("PublicMethod1"); }         This. Alertprivatename =function() {alert (privatename);    Privatemethod (); }}baseclass.prototype.publicname2= "Public2"; BASECLASS.PROTOTYPE.PUBLICMETHOD2=function() {alert ("PublicMethod2");} Baseclass.staticname= "Static"; Baseclass.staticmethod=function() {alert ("Staticmethod");} MyClass=function(){    varme =NewBaseClass (); Me.getname=function()    {        //cannot access the private properties and methods of the base classalert (privatename);    Privatemethod (); }        //define a public method for a subclassMe.othermethod =function() {alert ("Othermethod"); }        returnme;}

//Through prototype defined method, cannot access
MYCLASS.PROTOTYPE.OTHERMETHOD2 = function ()
{
Alert ("OtherMethod2");
};

New MyClass (); // access to base class public properties alert (object.pubicname1); alert (object.publicname2); // can access base class static properties alert (object.constructor.staticName); // access to the base class public method object.publicmethod1 (); Object.publicmethod2 (); // access to base class static methods Object.constructor.staticMethod (); // the private properties and methods of the base class can be accessed through the base class public method Object.alertprivatename ();

The effect of this inheritance is basically the same as the first, but it is important to note that this inheritance can only be done by adding properties and methods to me inside the function body, not using prototype, because when you return to new MyClass () it is actually the me that adds properties and methods, MyClass is just a shell.

JavaScript Object-oriented (3)--prototype and 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.