Di in the Angularjs

Source: Internet
Author: User

Di in the Angularjs
Always thought angular in the di is very tall things, also wrote a di demo, know that the difficulty is the last dynamic code execution: I now know the value of the parameter, but also know the method I want to execute/create the object's class, then in the case that the number of arguments is dynamic, How can I execute this method or create a new object? In my demo, I chose to use Eval.
But when I see the following statement in the ANGULARJS source code:

1 Switch(Self?-1: Args.length) {2          Case0:returnfn ();3          Case1:returnFN (args[0]);4          Case2:returnFN (Args[0], args[1]);5          Case3:returnFN (Args[0], args[1], args[2]);6          Case4:returnFN (Args[0], args[1], args[2], args[3]);7          Case5:returnFN (Args[0], args[1], args[2], args[3], args[4]);8          Case6:returnFN (Args[0], args[1], args[2], args[3], args[4], args[5]);9          Case7:returnFN (Args[0], args[1], args[2], args[3], args[4], args[5], args[6]);Ten          Case8:returnFN (Args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]); One          Case9:returnFN (Args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]); A          Case10:returnFN (Args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]); -         default:returnfn.apply (self, args); -}


A moment of intoxication. Angularjs unexpectedly enumerates 11 kinds of cases, then chooses the corresponding situation according to the parameter number. I think it's the stupidest and the stupidest way. But I don't think it's going to be more experienced than Google's guys, so there's got to be a reason. Check out the information found:
1. In strict mode, Eval doesn't work.
2. Both fn.apply () and Fn.call () are slower than FN ()
So my Getbean method in the Di of JavaScript should read:

1     //Core Approach2di.getbean=function(beanname) {3         //look at the cache, and some words go directly back4         if(beans[beanname]!=undefined)5             returnBeans[beanname];6Console.log ("haha" +beanname);7         varfn= This. Config[beanname];8         if(fn==undefined)return;9         varargs=getarguments (FN);Ten         if(!args) args=[]; One         varargss=NewArray (); A         varObjstr= ""; -         varIndex=0; -         varobj; the          for(varIinchargs) { -argss[i]=(Di.getbean (args[i])); -objstr+= "argss[" +index+ "],"; -index++; +         } -Objstr=objstr.substring (0, Objstr.length-1); +Objstr= "Obj=new (FN) (" +objstr+ "); "; A         /*The whole method can be implemented by this eval method, at 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 .*/ -         Switch(args.length) { -              Case0:obj=NewFN (); Break; -              Case1:obj=NewFN (Argss[0]); Break; -              Case2:obj=NewFN (argss[0],argss[1]); Break; in              Case3:obj=NewFN (argss[0],argss[1],argss[2]); Break; -             default: toeval (OBJSTR); Break; +         } -beans[beanname]=obj; the         returnobj; *};

Di in the Angularjs

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.