Skip to content

Instantly share code, notes, and snippets.

@mhayes
Last active September 15, 2015 22:59
Show Gist options
  • Save mhayes/87ce07eb7520f6eb00b1 to your computer and use it in GitHub Desktop.
Save mhayes/87ce07eb7520f6eb00b1 to your computer and use it in GitHub Desktop.
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