tirsdag den 23. oktober 2007

Multiple Inheritance in Javascript

The interesting stuff goes on in the definition of DerivedObj.
Note that BaseObj2 overrides the method fun1 that was first inherited from BaseObj1.
I wonder how this approach compares performancewise to inheritance on the prototype object.


function BaseObj1(arg1, arg2, arg3)
{
this.prop1 = arg1,
this.prop2 = arg2,
this.prop3 = arg3,
this.fun1 = function()
{
alert(prop1+prop2+prop3)
}
}

function BaseObj2()
{
this.fun1 = function()
{
return "this is not the same "+
"method as in BaseObj2"
},
this.prop5 = "I'm so static it hurts";
}

function DerivedObj(arg1, arg2, arg3, arg4)
{
this.prop4= arg4,
//inherit BaseObj1
BaseObj1.call(this, arg1, arg2, arg3),
//inherit BaseObj2
BaseObj2.call(this);
}