The implementation of polymorphism can adopt similar methods as inheritance. First, define an abstract class, in which some virtual methods are called. The virtual methods are not defined in the abstract class, but are implemented through its concrete implementation class.
As in the following example:
Object.extend=function(destination,source){ for(property in source){ destination[property]=source[property]; } return destination; } //Define an abstract base class base with no constructor function base(){}; base.prototype={ initialize:function(){ this.oninit();//A virtual method was called} } 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"First, an abstract base class base is defined. The oninit method is called in the initialize method of the base class, but the base class does not use the implementation or declaration of the oninit method. SubClassA and SubClassB classes inherit from base classes and implement the oninit method in different ways.