Last active
August 29, 2015 14:01
-
-
Save whunter/84bf0812429391c9fac8 to your computer and use it in GitHub Desktop.
prototypical inheritance
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 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