Skip to content

Instantly share code, notes, and snippets.

@mixu
Created January 4, 2012 12:54
Show Gist options
  • Save mixu/1559933 to your computer and use it in GitHub Desktop.
Save mixu/1559933 to your computer and use it in GitHub Desktop.
Implementing classes in JS without Object.create
var Animal = function(name) {
this.name = name;
};
Animal.prototype.move = function(meters) {
console.log(this.name+" moved "+meters+"m.");
};
var Snake = function() {
Animal.apply(this, Array.prototype.slice.call(arguments));
};
Snake.prototype = new Animal();
Snake.prototype.move = function() {
console.log("Slithering...");
Animal.prototype.move.call(this, 5);
};
var Horse = function() {
Animal.apply(this, Array.prototype.slice.call(arguments));
};
Horse.prototype = new Animal();
Horse.prototype.move = function() {
console.log("Galloping...");
Animal.prototype.move.call(this, 45);
};
var sam = new Snake("Sammy the Python")
, tom = new Horse("Tommy the Palomino")
;
sam.move()
tom.move()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment