Created
November 23, 2013 05:24
-
-
Save bluepnume/7611141 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 util = require('util'); | |
| // Let's create a class with some abstract methods | |
| function Animal() { | |
| console.log('A new', this.name, 'is born!') | |
| } | |
| Animal.prototype.talk = function() { | |
| console.log('The', this.name, 'goes', this.action) | |
| } | |
| // Now let's extend our abstract class | |
| function Cat() { | |
| } | |
| util.inherits(Cat, Animal); | |
| Cat.prototype.name = 'cat'; | |
| Cat.prototype.action = 'miaow'; | |
| function Kitten() { | |
| } | |
| util.inherits(Kitten, Cat); | |
| Kitten.prototype.name = 'kitten'; | |
| function Dog() { | |
| } | |
| util.inherits(Dog, Animal); | |
| Dog.prototype.name = 'dog'; | |
| Dog.prototype.action = 'woof'; | |
| // Now, let's generate some new objects using our classes | |
| var mycat = new Cat; | |
| var mykitten = new Kitten; | |
| var mydog = new Dog; | |
| mydog.action = 'woof woof woof' | |
| // Now we can call the abstract methods for each of our objects | |
| mycat.talk(); | |
| mykitten.talk(); | |
| mydog.talk(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment