Skip to content

Instantly share code, notes, and snippets.

@GarrettS
Created August 23, 2014 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GarrettS/5d52071c13bef2250322 to your computer and use it in GitHub Desktop.
Save GarrettS/5d52071c13bef2250322 to your computer and use it in GitHub Desktop.
Prototype Inheritance - extends.
function Car(make) {
this.make = make;
}
Car.prototype.drive = function() {
console.log('driving ' + this.make);
};
function Toyota() {
Car.call(this, "Toyota");
}
function F(){}
// The basis for "extends".
// F.prototype = Car.prototype;
// Toyota.prototype = new F;
var tt = new Toyota();
function extend(child, parent, options) {
function F() {}
F.prototype = parent.prototype;
child.prototype = new F;
for(var prop in options) {
child.prototype[prop] = options[prop];
}
return child;
}
var Toyota = extend(Toyota, Car, {
honk: function() {
console.log("honking " + this.make);
}
});
// var tt = new Toyota();
// tt.honk();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment