1.0.2 • Published 8 years ago

unwrap-this v1.0.2

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

unwrap-this

Coverage Status Build Status

NPM

Unwrap this context of a method and pass it as a parameter.
Useful when passing callbacks to third-party modules/libraries.

Installation

npm i unwrap-this --save

Usage

var unwrap = require('unwrap-this');

function onEventHappened(self, ...params) {
    // ctx passed as a param instead of being set to `this`.
}

var config = {
    option1: 'test',
    // The lib will call onSomeEvent() with some `this` ctx.
    onSomeEvent: unwrap(onEventHappened)
};

A more detailed example could be

var unwrap = require('unwrap-this');

function MyClass() {
    this.configOfThirdPartyLib = {
        opt1: true,
        opt2: false,
        onReady: unwrap(this.readyCallback.bind(this))
    };
    
    this.prop1 = 'some prop value';
}

MyClass.prototype.doSomething = function() {};

MyClass.prototype.readyCallback = function(tpInstance, ...params) {
    // Can access myClass instance's methods/props AND ctx of the caller.
    this.doSomething(tpInstance.name);
}
1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago