Skip to content

Instantly share code, notes, and snippets.

@mhayes mhayes/inheritance.js
Last active Sep 15, 2015

Embed
What would you like to do?
Simple javascript inheritance (as i currently understand it)
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
You can’t perform that action at this time.