npm.io
0.1.0 • Published 12 years ago

yiwn-inherit

Licence
Version
0.1.0
Deps
1
Vulns
0
Weekly
0

inherit

Inherit functionality and setup prototype chain from subclasses.

Installation

Using component

$ component install yiwn/inherit

Using npm for browserify

$ npm install yiwn-inherit

Usage

Example:

var inherit = require('yiwn-inherit');

function Parent(b){
    this.a = 'Aaa!',
    this.b = b;
    return this;
}

Parent.extend = inherit.bind(null, Parent);

// 
var Child = Parent.extend({
        print: function(){
            console.log(this.a, this.b);
        }
    });

var child = new Child('Boo!');

child.print(); // -> 'Aaa!', 'Boo!'

Supports replacement of constructor function as third argument.

var Child = Parent.extend({
        print: function() {
            console.log(this.a, this.b)
        }
    }, init);

function init(b) {
    this.b = b + '!!!';
    return this;
}

var child = new Child('Boo!');

child.print(); // -> undefined, 'Boo!!!!'

Test

Run tests with mocha

$ make test

Origins

Script replicates functionality of Backbone's .extend, extracted from Koboldmaki.

License

The MIT License

Keywords