Created
October 29, 2014 08:57
-
-
Save nielk/17bea9a61b2dd16f3c6d to your computer and use it in GitHub Desktop.
OOP javascript object inheritance constructor prototype
This file contains hidden or 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
/** | |
* Animal constructor | |
**/ | |
var Animal = function(name) { | |
this.name = name; | |
}; | |
Animal.prototype.speak = function() { | |
console.log('my name is ' + this.name); | |
}; | |
/** | |
* Dog constructor | |
**/ | |
var Dog = function(name) { | |
Animal.call(this, name); // super | |
}; | |
// inherite from Animal constructor | |
Dog.prototype = new Animal(); | |
Dog.prototype.bark = function() { | |
console.log('wouf wouf'); | |
}; | |
var puppy = new Dog('alpine'); | |
puppy.speak(); | |
puppy.bark(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment