Implementation of polymorphic adoption and inheritance similar methods in JS _javascript tips

Source: Internet
Author: User
Tags abstract extend inheritance

A polymorphic implementation can adopt and inherit similar methods. First, you define an abstract class, which invokes some virtual methods, which are not defined in the abstract class, but are implemented through their specific implementation classes.

As in the following example:

Object.extend=function (Destination,source) {for (property in 
source) { 
destination[property]=source[ Property]; 
return destination; 
} 
Defines an abstract base class base with no constructor function 
base () {}; 

base.prototype={ 
initialize:function () { 
this.oninit ();//called a virtual method 
} 
} 
function Subclassa () { 
//constructor 
} 
subclassa.prototype=object.extend ({ 
Propinsubclassa: "Propinsubclassa", 
Oninit:function () { 
alert (This.propinsubclassa); 
} 
},base.prototype); 

function Subclassb () { 
//constructor 
} 
subclassb.prototype=object.extend ({ 
PROPINSUBCLASSB: " Propinsubclassb ", 
oninit:function () { 
alert (THIS.PROPINSUBCLASSB); 
} 
},base.prototype); 

var obja=new subclassa (); 
Obja.initialize ()//Output "Propinsubclassa" 

var objb=new subclassb (); 
Objb.initialize ()//output "PROPINSUBCLASSB"

The

First defines an abstract base class base and calls the OnInit method in the Initialize method of the base class, but the base class does not use the OnInit method's implementation or declaration. The Subclassa and SUBCLASSB classes inherit from the base class and are implemented in different ways for the OnInit method.

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.