https://github.com/mattecapu/add-as-methods
Microutil to add bound functions to a JS object
https://github.com/mattecapu/add-as-methods
micro-library utility
Last synced: 4 months ago
JSON representation
Microutil to add bound functions to a JS object
- Host: GitHub
- URL: https://github.com/mattecapu/add-as-methods
- Owner: mattecapu
- Created: 2017-02-02T14:58:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-02T15:38:08.000Z (over 9 years ago)
- Last Synced: 2025-10-28T05:27:54.411Z (7 months ago)
- Topics: micro-library, utility
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# add-as-methods
### Microutil to add bound functions to a JS object
## Installation
```
npm install add-as-methods --save
```
## Usage
#### `addAsMethods(object, ...funcs)`
* `object` an object to augment with some methods
* `funcs` a list of _named_ functions to add to `object` as methods. The magic is simply "pass `this` as the first argument". Other arguments are also preserved (see example).
```js
import addAsMethods from 'add-as-methods';
function isEqual(fruit, anotherFruit) {
return fruit.name === anotherFruit.name;
}
function isBanana(fruit) {
return fruit.name === 'banana';
}
let awesomeFruit = {
name: 'banana',
awesome: true
};
let yourFruit = {
name: 'apple',
awesome: false
};
/* augment it */
awesomeFruit = addAsMethods(awesomeFruit, isEqual, isBanana);
console.log(`My awesome fruit ${awesomeFruit.isBanana() ? 'is' : 'isn\'t'} a banana`);
console.log(`My awesome fruit ${awesomeFruit.isEqual(yourFruit) ? 'is' : 'isn\'t'} the same as yours`);
```
## License
MIT