Created
November 26, 2014 15:10
-
-
Save ivanarrizabalaga/cc75a869108f906e24f2 to your computer and use it in GitHub Desktop.
Javascript subclassing pattern using prototypes
This file contains 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
//Super class | |
var Car = function(loc){ | |
this.loc=loc; | |
}; | |
Car.prototype.move=function(){ | |
this.loc++; | |
}; | |
//Sub class | |
var Van = function (loc){ | |
Car.call(this.loc); | |
} | |
Van.prototype=Object.create(Car.prototype); | |
Van.prototype.constructor=Van; | |
Van.prototype.grab=function(){ | |
//whatever | |
} | |
var zed = new Car(3); | |
zed.move(); | |
var amy=new Van(9); | |
amy.move(); | |
amy.grab(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment