Skip to content

Instantly share code, notes, and snippets.

@whunter
Last active August 29, 2015 14:01
Show Gist options
  • Save whunter/84bf0812429391c9fac8 to your computer and use it in GitHub Desktop.
Save whunter/84bf0812429391c9fac8 to your computer and use it in GitHub Desktop.
prototypical inheritance
var Animal = {
walk: function() {
console.log('walk');
},
talk: function() {
console.log('talk');
}
};
function Dog() {
this.talk = function() {
console.log('bark');
};
};
function Cat() {
this.talk = function() {
console.log('meow');
};
};
Dog.prototype = Animal;
Cat.prototype = Animal;
var dog = new Dog();
dog.walk(); // "walk"
dog.talk(); // "bark"
var cat = new Cat();
cat.walk(); // "walk"
cat.talk(); // "meow"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment