Learning Angularjs One of the reasons is that his di is very cow fork, in order to better study, in the study of the source code, I want to follow their own ideas to achieve a Di, I hope this idea can
Learning the source code is helpful.
1(function(){2 varconfig;3 varDi={};4 //used to cache objects that have been generated5 varbeans=NewArray ();6di.config=function(conf) {7 This. config=conf;8 returndi;9 };Ten One /*gets the argument list of a method that requires all injected arguments to start with $ A The second regular expression is the back of the G is very important, it means that the search to the first and then backward search - without this g, you can only match the first parameter. - The parameter list here refers to the list of parameters written in the code, not the parameter class table after the parameter is brought in. the For example, there is a method: - function Fn (a,b,c,c1) {} - The argument list is an array of four strings: ["A", "B", "C", "C1"] - because the name of the parameter is required to match the type of the parameter + */ - vargetarguments=function(fun) { + returnFun.tostring (). Match (/\ (. *\)/) [0].match (/\$\w+/g); A }; at //Core Approach -di.getbean=function(beanname) { - //look at the cache, and some words go directly back - if(beans[beanname]!=undefined) - returnBeans[beanname]; in in varfn= This. Config[beanname]; - if(fn==undefined)return; to varargs=getarguments (FN); + varargss=NewArray (); - varObjstr= ""; the varIndex=0; * varobj; $ for(varIinchargs) {Panax Notoginsengargss[i]=(Di.getbean (args[i])); -objstr+= "argss[" +index+ "],"; theindex++; + } AObjstr=objstr.substring (0, Objstr.length-1); theObjstr= "Obj=new (FN) (" +objstr+ "); "; + /*The whole di can be achieved by this eval method, - it takes a string parameter and compiles and executes the contents of the string according to the Javasript standard. $ The most important thing is that the variables inside the eval also follow JavaScript's function scope: that is, variables can be defined outside of Eval . $ */ - /* - of course, you can also use apply, or call, but there is always such a problem, it may be that they do not understand the two methods the use eval First, then study apply and call - */Wuyi eval (objstr); thebeans[beanname]=obj; - returnobj; Wu }; -Window.di=di; About}) (window);
Here's an example,
1 functionPerson () {2 This. Name= "Mike";3 This. address= "China";4 5 This. getname=function(){6 return This. Name;7 };8 This. getaddress=function(){9 return This. Address;Ten }; One } A functionService ($person) { - This. work=function(){ - return$person. GetName () + "is living in" +$person. getaddress (); the }; - } - - functionAdaptor ($person, $service) { + This. alertt=function(){ - vark=$service. Work (); + alert (k); A }; at}
There are person,service,adaptor three classes, the service class relies on the person to assemble the statement, the adaptor class relies on the Service class to display, as long as the following code will be able to run the
var conf={ "$person":P Erson, "$adaptor": adaptor, "$service" : Service};d i.config (conf). Getbean ("$adaptor"). Alertt ();
View Code
The current flaw in this di is that it cannot be assembled to determine the state of a class. For example, when you start new person, you want to assign a value to name by constructing the method, and the above method cannot be done directly.
It can only be done by one grounding method, as follows:
functionPerson () { This. Name= "Mike"; This. getname=function(){ return This. Name; }; This. setname=function($name) { This. name=$name. Name; };}functionName () { This. Name= "Tom";}//then re-assign the name to the person before using the person in the servicefunctionService ($person, $name) { This. work=function() {$person. SetName ($name. Name); return$person. GetName () + "is living in China";//This is Tom's living in China };}
View Code
The di of JavaScript