Created
August 23, 2014 04:01
-
-
Save GarrettS/5d52071c13bef2250322 to your computer and use it in GitHub Desktop.
Prototype Inheritance - extends.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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