Socket
Socket
Sign inDemoInstall

add-as-methods

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    add-as-methods

Microutil to add bound functions to a JS object


Version published
Maintainers
1
Install size
2.75 kB
Created

Readme

Source

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

FAQs

Last updated on 02 Feb 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc