Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A prototype inheritance helper
Prototyper = {};
Prototyper.create = function (base) {
var empty = function () {};
empty.prototype = base;
return new empty();
};
Prototyper.xtends = function (parent, instance) {
instance.prototype = this.create(parent.prototype);
instance.prototype.constructor = instance;
instance.prototype.__super__ = parent;
};
var Car = function(){
console.log("Car construct");
};
Car.prototype.drive = function () {
console.log("brumm");
};
var myCar = Prototyper.create(Car.prototype);
console.dir(myCar);
// What should work
var FourWheeler = function() {
this.__super__();
console.log("FourWheeler construct");
};
Prototyper.xtends(Car, FourWheeler);
FourWheeler.prototype.driveOffroad = function () {
console.log("FourWheeler driveOffroad");
}
var jeep = new FourWheeler();
jeep.drive(); //brumm
jeep.driveOffroad(); //FourWheeler driveOffroad
console.log(jeep instanceof FourWheeler); // true
console.log(jeep instanceof Car); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.