Last active
August 29, 2015 14:09
-
-
Save dinks/911d30d2be93322ff062 to your computer and use it in GitHub Desktop.
Parasitic Combination Inheritance Pattern
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
| 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