I have a simple class inheritance in JS:
var Class = function(){};
Class.extend = function(){
var Extended = function(constructor){
function extend_obj(destination, source) {
for(var property in source){
if(typeof source[property] === "object" && source[property] !== null && destination[property]){
extend_obj(destination[property], source[property]);
}else{
destination[property] = source[property];
}
}
};
extend_obj(Extended.prototype, constructor);
this.constructor.init();
};
Extended.init = function(){};
for(var key in this.prototype){
Extended.prototype[key] = this.prototype[key];
}
Extended.prototype.constructor = Extended;
for (var key in this) {
Extended[key] = this[key];
};
return Extended;
};
Which allow me to do so:
var SubClass = Class.extend();
SubClass.static_property = "a static property";
SubClass.static_method = function(){
return "a static method";
};
SubClass.prototype.a_new_property = "something new"; //instance method
var instance = new SubClass({
name: "custom name"
});
SubClass.init = function(){
//custom constructor
};
this.constructor.static_method_or_property //call instance method
I need:
- multiple inheritance
- the possibility to use module pattern
There's a cleaner/better/faster way to do that?