Skip to content

Instantly share code, notes, and snippets.

@ivanarrizabalaga
Created November 26, 2014 15:10
Show Gist options
  • Save ivanarrizabalaga/cc75a869108f906e24f2 to your computer and use it in GitHub Desktop.
Save ivanarrizabalaga/cc75a869108f906e24f2 to your computer and use it in GitHub Desktop.
Javascript subclassing pattern using prototypes
//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