Initial commit
[yaffs-website] / node_modules / globule / node_modules / inherits / README.md
1 A dead simple way to do inheritance in JS.
2
3     var inherits = require("inherits")
4
5     function Animal () {
6       this.alive = true
7     }
8     Animal.prototype.say = function (what) {
9       console.log(what)
10     }
11
12     inherits(Dog, Animal)
13     function Dog () {
14       Dog.super.apply(this)
15     }
16     Dog.prototype.sniff = function () {
17       this.say("sniff sniff")
18     }
19     Dog.prototype.bark = function () {
20       this.say("woof woof")
21     }
22
23     inherits(Chihuahua, Dog)
24     function Chihuahua () {
25       Chihuahua.super.apply(this)
26     }
27     Chihuahua.prototype.bark = function () {
28       this.say("yip yip")
29     }
30
31     // also works
32     function Cat () {
33       Cat.super.apply(this)
34     }
35     Cat.prototype.hiss = function () {
36       this.say("CHSKKSS!!")
37     }
38     inherits(Cat, Animal, {
39       meow: function () { this.say("miao miao") }
40     })
41     Cat.prototype.purr = function () {
42       this.say("purr purr")
43     }
44
45
46     var c = new Chihuahua
47     assert(c instanceof Chihuahua)
48     assert(c instanceof Dog)
49     assert(c instanceof Animal)
50
51 The actual function is laughably small.  10-lines small.