bind-methods
Version:
Bind all methods in an object to itself or a specified context
58 lines (36 loc) • 883 B
Markdown
> Bind all methods in an object to itself or a specified context
```
$ npm install bind-methods
```
```js
import bindMethods from 'bind-methods';
const unicorn = {
name: 'Rainbow',
message() {
return `${this.name} is awesome!`;
}
};
const message = unicorn.message;
message();
//=> Error: Cannot read property 'name' of undefined
bindMethods(unicorn);
const message2 = unicorn.message;
message2();
//=> 'Rainbow is awesome!'
```
Bind methods in `input` to itself or `context` if specified.
Returns the `input` object.
Type: `object`
Object with methods to bind.
Type: `object`\
Default: The `input` object
Object to bind the methods to.
- [auto-bind](https://github.com/sindresorhus/auto-bind) - Automatically bind methods to their class instance