I've been reading up on inheritance in Javascript and have come up with creating constructors and defining each constructors prototypes. I've created a prototypeExtend function that will loop through each constructors prototypes and add them to the main constructors prototypes.
Is this a practical way to achieve inheritance?
function prototypeExtend() {
var cMain = arguments[0].prototype;
for(var i=1; i<arguments.length; i++){
var cTemp = arguments[i].prototype;
for(var k in cTemp){
if(cTemp.hasOwnProperty(k)){
cMain[k] = cTemp[k];
}
}
}
return cMain;
}
function Class1 () {}
Class1.prototype = {
el1:null,
fun1:function(str){
console.log(str + " from Class1");
},
setEl1:function(value){
this.el1 = value;
},
getEl1:function(){
return this.el1;
}
}
function Class2(){}
Class2.prototype = {
el2:"blah",
fun2:function(str){
console.log(str + " from Class2");
}
}
function Class3(){}
Class3.prototype = {
el3:"dah",
fun3:function(str){
console.log(str + " from Class3");
},
fun1:function(str){
console.log(str + " from Class3");
}
}
prototypeExtend(Class2, Class1, Class3);
var obj = new Class2;
console.log(obj);