Created
November 11, 2013 19:17
-
-
Save thelinuxlich/7418706 to your computer and use it in GitHub Desktop.
Herança em JS do jeito certo
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
function Animal(name) { | |
this.name = name; | |
}; | |
Animal.prototype.move = function(meters) { | |
console.log(this.name+" moved "+meters+"m."); | |
}; | |
function Snake() { | |
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 sam = new Snake("Sammy the Python"); | |
sam.move(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment