delegate-this

0.1.0 • Public • Published

delegate-this

npm

super simple object delegation

No classes here. Use JavaScript as it was intended, i.e., simply objects extending other objects.

The only deviation is that this implementation allows for the use of this.super(...args) in any function to call its hidden "parent" function (with or without function arguments).

Examples

const d = require('delegate-this');

const base = {
    derp() {
        console.log('i am derp');
    },
    sum(list) {
        console.log(
            list.reduce((acc, curr) => (
                acc += curr, acc
            ))
        );
    }
};

const foo = d.create(base, {
    derp() {
        console.log('foo');
        this.super();
    },
    herp() {
        console.log('i am herp');
    }
});

const bar = d.create(foo, {
    derp() {
        console.log('bar');
        this.super();
    },
    sum(...list) {
        // do stuff
        this.super(list);
    }
});

bar.derp(); // 'bar', 'foo', 'i am derp'
bar.herp(); // 'i am herp'
bar.sum(5, 10, 15, 20); // 50

See another example.

License

GPLv3

Author

Benjamin Toll

Package Sidebar

Install

npm i delegate-this

Weekly Downloads

1

Version

0.1.0

License

GPLv3

Last publish

Collaborators

  • benjam72