basic-injector
v1.1.3
Published
Automatically call function with the appropriate parameters according to the parameters name in function signature.
Downloads
3
Maintainers
Readme
BasicInjector
BasicInjector is a javascript module that allows you to call your functions without sending them their arguments. The Injector class allows you to set a parameter dictionary, so that when you call your function using inject, the appropriate parameter wiil be passed according to the parameter names in the function signature.
Basic Usage
Use injector to inject parameters to function
$ npm install basic-injector
const Injector = require('basic-injector');
var injector = new Injector({
config: require('./config'),
env: 'production',
utils: require('./utils')
});
injector.inject((utils, config, env) => {
console.log(env) // production
});
Adding parameters
Add more parameters to inject
injector.set("port", 8080);
Removing parameters
Remove parameters to inject
injector.remove("port");
Set thisArg
Set the fucntions this
var obj = {};
injector.inject(function(port){
this.port = port;
}, obj);
console.log(obj) // { port: 8080 }
Use function as constructor
Run the function with the new keyword to create an instance of it
var Example = function(port){
this.port = port;
}
var example = injector.construct(Example);
console.log(example) // Example { port: 8080 }