Skip to content

Instantly share code, notes, and snippets.

@dinks
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save dinks/911d30d2be93322ff062 to your computer and use it in GitHub Desktop.

Select an option

Save dinks/911d30d2be93322ff062 to your computer and use it in GitHub Desktop.
Parasitic Combination Inheritance Pattern
Object.create = function(o) {
function F() {};
F.prototype = o;
return new F();
};
var inheritP = function(child, parent) {
parentDup = Object.create(parent.prototype);
parentDup.constructor = child;
child.prototype = parentDup;
}
function Vehicle(tyres) {
this.tyres = tyres;
}
Vehicle.prototype.describe = function() {
console.log('A Vehicle with '+ this.tyres + ' Tyres!');
}
function Car() {
Vehicle.call(this, 4);
}
inheritP(Car, Vehicle);
function Scooter() {
Vehicle.call(this, 2);
}
inheritP(Scooter, Vehicle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment