0.1.15 • Published 10 years ago

better-inherits v0.1.15

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

Deprecated

Please use the class keyword with:


Improved util.inherits() and Klass pattern

npm install better-inherits

How to use inherits()

    var inherits = require('better-inherits');

    function Point(x, y) {
        this.x = x;
        this.y = y;
    }
    Point.prototype = {
        length: function () {
            return Math.sqrt(this.x * this.x + this.y * this.y);
        }
    };

    function Circle(x, y, r) {
        this._base(x, y); // Point.call(this, x, y);
        this.r = r;
    }
    /*Circle.prototype = */inherits(Circle, Point, {
        area: function () {
            return Math.PI * this.r * this.r;
        }
    });

    var circle;

    it('instance creation',function() {
        circle = new Circle(1, 2, 3);
        circle.x.should.equal(1);
        circle.y.should.equal(2);
        circle.r.should.equal(3);
    });

    it('instance of Point', function() {
        circle.should.instanceOf(Point);
    });

    it('instance of Circle', function() {
        circle.should.instanceOf(Circle);
    });

    it('own method', function() {
        circle.area().should.equal(28.274333882308138);
    });

    it('inherited method', function() {
        circle.length().should.equal(2.23606797749979);
    });

Klass pattern

var Class = require('better-inherits').Class;

var Point = new Class({
    constructor: function(x, y) {
        this.x = x;
        this.y = y;
    },
    length: function() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }
});

var Circle = new Class({
    prototype: Point,
    constructor: function(x, y, r) {
        this._base(x, y); // Point.call(this, x, y);
        this.r = r;
    },
    area: function() {
        return Math.PI * this.r * this.r;
    }
});

var circle;

it('instance creation',function() {
    circle = new Circle(1, 2, 3);
    circle.x.should.equal(1);
    circle.y.should.equal(2);
    circle.r.should.equal(3);
});

it('instance of Point', function() {
    circle.should.instanceOf(Point);
});

it('instance of Circle', function() {
    circle.should.instanceOf(Circle);
});

it('own method', function() {
    circle.area().should.equal(28.274333882308138);
});

it('inherited method', function() {
    circle.length().should.equal(2.23606797749979);
});

How to build and test

npm install
gulp build
gulp test
0.1.15

10 years ago

0.1.14

10 years ago

0.1.10

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago