I think this is much better than init hax in a simple fw inheritance:
(function() { var core = { require : function(source) { if ( typeof (source) != "object" || !source) throw new TypeError("Object needed as source."); for (var property in source) if (source.hasOwnProperty(property) && !this.prototype.hasOwnProperty(property)) this.prototype[property] = source[property]; }, override : function(source) { if ( typeof (source) != "object" || !source) throw new TypeError("Object needed as source."); for (var property in source) if (source.hasOwnProperty(property)) this.prototype[property] = source[property]; }, extend : function(source) { var superClass = this; var newClass = source.hasOwnProperty("constructor") ? source.constructor : function() { superClass.apply(this, arguments); }; newClass.superClass = superClass; var superClone = function() { }; superClone.prototype = superClass.prototype; newClass.prototype = new superClone(); newClass.prototype.constructor = newClass; if (source) newClass.override(source); return newClass; } }; core.require.call(Function, core); Function.create = function (source){ var newClass = source.hasOwnProperty("constructor") ? source.constructor : function() {}; newClass.override(source); return newClass; }; })();
An example of a car with this:
var Vehicle = Function.create({ constructor : function(wheels) { this.wheels = wheels; } }); var Truck = Vehicle.extend({ constructor : function(hp, wheels) { this.horsepower = hp; Vehicle.call(this, wheels); }, printInfo : function() { console.log('I am a truck and I have ' + this.horsepower + ' hp.'); } }); var t = new Truck(4, 350); t.printInfo();
source share