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.