Created
January 4, 2012 12:54
-
-
Save mixu/1559933 to your computer and use it in GitHub Desktop.
Implementing classes in JS without Object.create
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
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