Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/atlassubbed/atlas-bind-methods
https://github.com/atlassubbed/atlas-bind-methods
Last synced: 21 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/atlassubbed/atlas-bind-methods
- Owner: atlassubbed
- License: other
- Created: 2018-06-24T22:06:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-28T05:10:07.000Z (over 6 years ago)
- Last Synced: 2024-12-15T04:06:49.660Z (30 days ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# atlas-bind-methods
Binds an instance's prototype methods to itself.
[![Travis](https://img.shields.io/travis/atlassubbed/atlas-bind-methods.svg)](https://travis-ci.org/atlassubbed/atlas-bind-methods)
---
## install
```
npm install --save atlas-bind-methods
```## why
Suppose you have a class which uses instance variables in its methods:
```javascript
// Animal.js
module.exports = class Animal {
constructor(sound){
this.sound = sound;
}
rawr(){
console.log(this.sound)
}
}
```Normally, the following won't work:
```javascript
const Animal = require("./Animal");
const cat = new Animal("meow!");
const rawr = cat.rawr;
rawr(); // TypeError: Cannot read property 'sound' of undefined
```It fails because the reference loses the method's context, but you already knew that! The fix is pretty simple -- just bind the function to the instance:
```javascript
...
const rawr = cat.rawr.bind(cat)
rawr() // meow!
```But, this is kind of annoying when I have to do this for many methods. With this package, we can do all of the binding without cluttering our business logic.
## examples
#### inline
```javascript
const Animal = require("./Animal");
const bind = require("atlas-bind-methods");
const cat = bind(new Animal("meow!"));
const rawr = cat.rawr;
rawr() // meow!
```#### as a side-effect
```javascript
...
const cat = new Animal("meow!");
bind(cat);
const rawr = cat.rawr;
rawr() // meow!
```## caveats
This doesn't support subclassing, so it would only pre-bind the prototype methods of the direct constructor which is used to instantiate the object.