Last active
September 15, 2015 22:59
-
-
Save mhayes/87ce07eb7520f6eb00b1 to your computer and use it in GitHub Desktop.
Simple javascript inheritance (as i currently understand it)
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 Mammal = function (name) { | |
this.name = name; | |
}; | |
Mammal.prototype.sayHello = function () { | |
return "Hello, I'm a " + this.name; | |
} | |
var Cow = function (name) { | |
Mammal.apply(this, arguments); | |
}; | |
Cow.prototype = new Mammal(); | |
Cow.prototype.constructor = Cow; | |
// Usage example | |
var jerseyCow = new Cow("jersey cow"); | |
console.info(jerseyCow.sayHello()); | |
var magicCow = new Cow("magic cow"); | |
console.info(magicCow.sayHello()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment