Achieving polymorphism in javascript by prototype--simple example

Parvendra
Posted by Parvendra under JavaScript category on | Views : 4921
function mainfuncone()
{
this.prop_1 =27;

}

mainfuncone.prototype.polyfuncname=function()
{
this.prop_1 += 10;
}

function mainfunctwo()
{
this.prop_11 =30;

}

mainfunctwo.prototype.polyfuncname=function()
{
this.prop_11 +=10;

}

obj1=new mainfuncone;
obj2=new mainfunctwo;

//call functions
obj1.polyfuncname();
obj2.polyfuncname();

//access properties values by using polymorphism
alert(obj1.prop_1);
alert(obj2.prop_11);

Comments or Responses

Login to post response