2.0.0 • Published 9 years ago
add-as-methods v2.0.0
add-as-methods
Microutil to add bound functions to a JS object
Installation
npm install add-as-methods --saveUsage
addAsMethods(object, ...funcs)
objectan object to augment with some methodsfuncsa list of named functions to add toobjectas methods. The magic is simply "passthisas the first argument". Other arguments are also preserved (see example).
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