https://github.com/ashubham/unwrap-this
unwrap this context of a method and get it as a parameter.
https://github.com/ashubham/unwrap-this
Last synced: 22 days ago
JSON representation
unwrap this context of a method and get it as a parameter.
- Host: GitHub
- URL: https://github.com/ashubham/unwrap-this
- Owner: ashubham
- License: mit
- Created: 2016-08-12T05:47:22.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-08-12T09:13:51.000Z (over 8 years ago)
- Last Synced: 2025-03-18T09:24:02.298Z (about 1 month ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unwrap-this
[](https://coveralls.io/github/ashubham/unwrap-this?branch=master)
[](https://travis-ci.org/ashubham/unwrap-this)[](https://www.npmjs.org/package/unwrap-this)
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
```javascript
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
```javascript
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);
}
```