Sharing ideas in JavaScript to implement dependency injection

Source: Internet
Author: User
Tags reflection split

The following requirements:

Suppose you already have a defined service Module Key-value collection, Func for the new service you added, and the parameter list is a service dependency.

The code is as follows:

var services = {abc:123, def:456, ghi:789}; Let's say some service is defined.

function Service (ABC, GHI) {

This.write = function () {

Console.log (ABC);

Console.log (GHI);

}

}

function Activitor (func) {

var obj;

Realize

return obj;

}

Solution Idea:

By some mechanism (reflection?), remove the list of parameters defined by the Func and assign the values. And then through some mechanism (activitor), instantiate the Func.

Solution:

First, get the func parameter list:

How do I get a list of parameters? The first thing I thought of was the reflection mechanism. Is there any reflection in the JavaScript? Yes, I do. The eval (str) function is currently available, but it does not seem to get a related implementation of the argument list. Looking at the func.arguments definition, this property is only valid when calling Func and passing parameters, and does not satisfy the requirement.

Can you get a list of arguments by handling the string after func.tostring ()?

Try it out:

The code is as follows:

function Getfuncparams (func) {

var matches = func.tostring (). Match (/^functions*[^ (]* (s* ([^)]*))/m);

if (matches && matches.length > 1)

Return Matches[1].replace (/s*/, '). Split (', ');

return [];

};

This gets an array of func argument lists.

Second, based on the list of parameters to find dependencies:

You get the list of arguments, you get a list of dependencies, and it's easy to pass dependencies as arguments.

The code is as follows:

var params = Getfuncparams (func);

for (var i in params) {

Params[i] = services[params[i]];

}

Third, pass the dependency parameter and instantiate it:

We know that there are func.constructor in JavaScript with call (thisarg,[arg[,arg,[arg,[...)]] and apply (Thisarg,args ...) Two functions, you can implement an instantiated func operation. Where the call function's first argument is the this pointer, and the rest is the argument list, which is suitable for use in the case of a known func parameter list, and does not meet my needs. Look at the second apply function, the first argument is the this pointer, and the second argument is an array of arguments, which is automatically assigned to Func's argument list one by one when invoked, just to meet my needs.

The code is probably as follows:

The code is as follows:

function Activitor (func) {

var obj = {};

Func.apply (obj, params);

return obj;

}

At this point we were able to create an instance of the Func and pass the parameters that the Func needed.

Four, print test it:

Complete code:

The code is as follows:

Var

Let's say some service is defined.

Services = {abc:123, def:456, ghi:789},

Gets the argument list for Func (dependent list)

Getfuncparams = function (func) {

var matches = func.tostring (). Match (/^functions*[^ (]* (s* ([^)]*))/m);

if (matches && matches.length > 1)

Return Matches[1].replace (/s+/, '). Split (', ');

return [];

},

Fill parameters (dependencies) based on the argument list (dependent list)

Setfuncparams = function (params) {

for (var i in params) {

Params[i] = services[params[i]];

}

return params;

};

Activating device

function Activitor (func) {

var obj = {};

Func.apply (obj, Setfuncparams (Getfuncparams (func)));

return obj;

}

Define new service

function Service (ABC, GHI) {

This.write = function () {

Console.log (ABC);

Console.log (GHI);

}

}

Instantiate the service and invoke the method

var service = activitor (service);

Service.write ();

Console print successfully!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.