Wednesday, July 1, 2009

Inheritance using javascript.

After digging little inside in the prototype object. i have found can achieve inheritance. Really its nice to see the code in javascript. i will write some sample code. try to use your projects also.. In future am trying to implement javascript with the custom objects and methods. hope in future will see nice coding with javascript also.

Example

function Base()
{
this.a = "Bala";
this.b = "Bala1";
}
function Derive()
{
this.a = "Bala2";
}
function ShowAll()
{
Derive.prototype = new Base();
var show = new Derive();
alert(show.a);
alert(show.b);
}
<body onload="javascript:ShowAll()">

Output

Bala2
Bala1