Skip to content

mattecapu/add-as-methods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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).
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

About

Microutil to add bound functions to a JS object

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published