Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Dependency Injection

I am new at JavaScript. I wonder how dependency injection is being implemented in JavaScript? I searched the internet but couldn't find anything.

like image 726
Levent Sezer Avatar asked Nov 18 '13 21:11

Levent Sezer


People also ask

Is there dependency injection in JavaScript?

Dependency injection (DI) is a programming pattern in which a dependency is passed using the parameters instead of instantiating it within the function or class.

What is injection in JavaScript?

A JavaScript injection attack is a type of attack in which a threat actor injects malicious code directly into the client-side JavasScript. This allows the threat actor to manipulate the website or web application and collect sensitive data, such as personally identifiable information (PII) or payment information.

How does dependency injection help in JavaScript based applications?

Dependency injection is a software design pattern that allows someone to remove hard-coded dependencies and makes it possible to change them. Dependencies can be injected to the object via the constructor or via defined method or a setter property.

What is dependency JavaScript?

A dependency is some third-party code that your application depends on. Just like a child depends on its parent, your application depends on other people's code. A piece of code becomes a true dependency when your own application cannot function without it.


1 Answers

var Injector = {    dependencies: {},    add : function(qualifier, obj){       this.dependencies[qualifier] = obj;     },    get : function(func){       var obj = new func;       var dependencies = this.resolveDependencies(func);       func.apply(obj, dependencies);       return obj;    },    resolveDependencies : function(func) {       var args = this.getArguments(func);       var dependencies = [];       for ( var i = 0; i < args.length; i++) {          dependencies.push(this.dependencies[args[i]]);       }       return dependencies;    },    getArguments : function(func) {       //This regex is from require.js       var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;       var args = func.toString().match(FN_ARGS)[1].split(',');       return args;    } }; 

The first thing we need a configuration to provide necessary dependencies with qualifiers. To do that, we define a dependency set as dependencies in the Injector class. We use dependency set as our container which will take care of our object instances mapped to qualifiers. In order to add new instance with a qualifier to dependency set, we define an add method. Following that, we define get method to retrieve our instance. In this method, we first find the arguments array and then map those arguments to dependencies. After that, we just construct the object with our dependencies and return it. For more information and examples, please see the post on my blog.

like image 194
yusufaytas Avatar answered Oct 04 '22 13:10

yusufaytas