Security News
UK Officials Consider Banning Ransomware Payments from Public Entities
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
injector-js
Advanced tools
InjectorJS is a simple Dependency Injection Container for JavaScript. It helps you to remove hard-corded dependencies and make it possible to change them at any time. For more information about this pattern look at:
This docs describe many features of the library, but not all. Use source to learn InjectorJS better.
###Installation###
For installation read related chapter.
###Usage###
Online working version of fol;owing examples is available on plunker: http://plnkr.co/edit/SDXdO5. Feel free to play around with it!
Definition of simple objects (parameters):
console.log(injector.has('name')); // false
injector.set('name', 'Bob');
injector.set('PI', 3.14).set('currentTime', new Date());
console.log(injector.has('name')); // true
Defition of objects by factory functions:
injector.set('auto', function() {
return {
color: this.get('auto_color'),
type: this.get('auto_type')
});
Factory functions has access to current injector making it possible to references to other objects (inject dependencies). Objects are created only when you require them. These cause to that order of the definitions does not matter and there is no performance penalty.
You can define several objects in one set method call:
injector.set({
auto_color: function() {
return Math.random() < 0.5 ? 'black' : 'white';
},
auto_type: function() {
return Math.random() < 0.5 ? 'hatchback' : 'sedan';
},
auto: function() {
return {
color: this.get('auto_color'),
type: this.get('auto_type')
};
}
});
You can add object factory to injector to simplify creation of common type objects. Before creation object value will be passed to appropriate object factory:
injector.setFactory('x60', function(value) {
return value*60;
});
injector.set('hours_in_minutes', 'x60', function() {
return 5;
});
console.log(injector.get('hours_in_minutes')); // 300
injector.setFactory('join', {
create: function(strings) {
return strings.join(' ');
}
});
injector.set('hello', 'join', ['hello', 'world', '!']);
console.log(injector.get('hello')); // hello world !
As you see object factory can be a function or an object with create method. By default there are 3 object factories:
injector.set({
service: {
person: function() {
return {
class: this.get('person_class'),
init: [this.get('person_age')],
call: {
setName: [this.get('person_name')]
}
}
}
},
clazz: {
person_class2: function() {
return {
name: 'Some/Clazz/Name',
parent: 'Some/Clazz/Parent',
deps: [10,20,30]
}
}
},
person_class: function() {
var Person = function(age) {
this.name = undefined;
this.age = age;
};
Person.prototype.setName = function(name) {
this.name = name;
}
return Person;
},
person_age: 10,
person_name: 'Bob'
});
var person = injector.get('person');
console.log(person.name); // 'Bob'
console.log(person.age); // 10
Copyright (c) 2013 Aleksey Podskrebyshev. Licensed under the MIT license.
FAQs
Simple Dependency Injection Container for JavaScript
The npm package injector-js receives a total of 0 weekly downloads. As such, injector-js popularity was classified as not popular.
We found that injector-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.