Sha256: fc2512516a01dc7ca4c8d34cd77d11eedcb6b98acf19e45082c5154eec8e911c

Contents?: true

Size: 1.08 KB

Versions: 28

Compression:

Stored size: 1.08 KB

Contents

A dead simple way to do inheritance in JS.

    var inherits = require("inherits")

    function Animal () {
      this.alive = true
    }
    Animal.prototype.say = function (what) {
      console.log(what)
    }

    inherits(Dog, Animal)
    function Dog () {
      Dog.super.apply(this)
    }
    Dog.prototype.sniff = function () {
      this.say("sniff sniff")
    }
    Dog.prototype.bark = function () {
      this.say("woof woof")
    }

    inherits(Chihuahua, Dog)
    function Chihuahua () {
      Chihuahua.super.apply(this)
    }
    Chihuahua.prototype.bark = function () {
      this.say("yip yip")
    }

    // also works
    function Cat () {
      Cat.super.apply(this)
    }
    Cat.prototype.hiss = function () {
      this.say("CHSKKSS!!")
    }
    inherits(Cat, Animal, {
      meow: function () { this.say("miao miao") }
    })
    Cat.prototype.purr = function () {
      this.say("purr purr")
    }


    var c = new Chihuahua
    assert(c instanceof Chihuahua)
    assert(c instanceof Dog)
    assert(c instanceof Animal)

The actual function is laughably small.  10-lines small.

Version data entries

28 entries across 23 versions & 8 rubygems

Version Path
blackboard-3.1.9 lib/site_template/node_modules/globule/node_modules/inherits/README.md
gulp_assets-1.0.0.pre.5 template/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/node_modules/globule/node_modules/glob/node_modules/inherits/README.md
gulp_assets-1.0.0.pre.5 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/gaze/node_modules/globule/node_modules/glob/node_modules/inherits/README.md
gulp_assets-1.0.0.pre.4 template/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/node_modules/globule/node_modules/glob/node_modules/inherits/README.md
gulp_assets-1.0.0.pre.4 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/gaze/node_modules/globule/node_modules/glob/node_modules/inherits/README.md
gulp_assets-1.0.0.pre.3 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/gaze/node_modules/globule/node_modules/glob/node_modules/inherits/README.md
gulp_assets-1.0.0.pre.3 template/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/node_modules/globule/node_modules/glob/node_modules/inherits/README.md
entangled-0.0.16 spec/dummy/public/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
entangled-0.0.15 spec/dummy/public/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
entangled-0.0.14 spec/dummy/public/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
entangled-0.0.13 spec/dummy/public/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
entangled-0.0.12 spec/dummy/public/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
entangled-0.0.11 spec/dummy/public/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
entangled-0.0.10 spec/dummy/public/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
trans-0.5.10 template/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/node_modules/globule/node_modules/glob/node_modules/inherits/README.md
trans-0.5.10 template/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
trans-0.5.9 template/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/node_modules/globule/node_modules/glob/node_modules/inherits/README.md
trans-0.5.9 template/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
cssesc-source-0.1.0 vendor/node_modules/grunt/node_modules/glob/node_modules/inherits/README.md
stylus-source-0.34.1 vendor/node_modules/mocha/node_modules/glob/node_modules/inherits/README.md