The simple implementation of angularjs's array parameter passing method and angularjs Array

Source: Internet
Author: User

The simple implementation of angularjs's array parameter passing method and angularjs Array

When I was a beginner at angularjs, I was curious about the array parameter passing method (['A', 'B', function (a, B) {}]), how is it implemented? Later, due to busy work, I forgot about the problem.

Today, I have nothing to worry about. The simplest way is to view its source code. However, I am not good at E-text. If I don't talk about his design logic, it is enough for me to read only the English comments. The car was finally built by trying to build a closed door.

Now that you have created your own car, you need to bring your own name (take the first letter of the name in pinyin) and ask him to use mqyJs. The following is a demo call method:

Var app2 = mqyJs. applicationCreate ([{name: 'directly input SCOPE '},' $ hello', '$ world', function ($ scope, $ hello, $ world) {return $ scope. name + ":" + $ hello. name + $ world. name;}]);

The core part is as follows:

// The framework opens var mqyJs ={// Service Registration servicesList :{}, servicesRegister: function (name, value) {this. servicesList [name] = value;}, // create applicationList: [], applicationCreate: function (_ opts, _ args) {if (! _ Args) {_ args = _ opts; _ opts = {}}_ opts. scope = _ opts. scope | {name: 'scope not set '}; if (! (_ Args instanceof Array) {_ args = ['$ scope', _ args];} if (typeof _ args [_ args. length-1]! = 'Function') {throw new Error ('function is not specified in the parameter ');} _ args. map (arg, index) =>{ if (typeof arg = 'string') {if (arg = '$ scope ') {_ args [index] = _ opts. scope;} else {if (!! Arg &&! (Arg in this. servicesList) {throw new Error ('plug-in: '+ arg +' not registered yet ');} _ args [index] = this. servicesList [arg] ;}}); return this. applicationList [this. applicationList. push ({run: function (callback) {if (typeof callback! = 'Function') {callback = function (_ opts) {return _ opts;} return callback (_ args [_ args. length-1]. apply (null, _ args) ;}})-1] ;}}; // framework ended

Through servicesRegister, you can register services, such as the $ http of angularjs;

// The plug-in starts mqyJs. servicesRegister ('$ hello', {name: ''}); mqyJs. servicesRegister ('$ world', {name: 'World'}); mqyJs. servicesRegister ('$ China', {name: 'China'}); // plug-in ended

Finally, all registered applications are automatically executed.

/*** The system runs automatically after Initialization is complete. * For example, place the webpage in window. onload */mqyJs. applicationList. map (function (app, index) {console. log ('automatic call-> APP # '+ index +'-> '+ app. run ());});

Try to run the code to automatically identify the parameter type and execute it perfectly.

If $ scope is not input, the program automatically creates a $ scope.

// The Demo code starts var app = mqyJs. applicationCreate (['$ scope', '$ hello',' $ China', function ($ scope, $ hello, $ china) {return $ scope. name + ":" + $ hello. name + $ china. name;}]); var app2 = mqyJs. applicationCreate ([{name: 'directly input SCOPE '},' $ hello', '$ world', function ($ scope, $ hello, $ world) {return $ scope. name + ":" + $ hello. name + $ world. name;}]); var app3 = mqyJs. applicationCreate ([{name: 'World is also passed in directly '},' $ hello', {name: 'global'}, function ($ scope, $ hello, $ world) {return $ scope. name + ":" + $ hello. name + $ world. name;}]); var app4 = mqyJs. applicationCreate (function ($ scope) {return $ scope. name ;}); var opts = {scope: {name: 'custom SCOPE '}}; var app5 = mqyJs. applicationCreate (opts, function ($ scope) {return $ scope. name;}); app4.run (function (result) {console. log ('manually call-> RESULT-> '+ result) ;}); // end of the Demo code

To facilitate the test, re-write the code and directly copy the following code to the browser console for testing.

// The framework opens var mqyJs ={// Service Registration servicesList :{}, servicesRegister: function (name, value) {this. servicesList [name] = value;}, // create applicationList: [], applicationCreate: function (_ opts, _ args) {if (! _ Args) {_ args = _ opts; _ opts = {}}_ opts. scope = _ opts. scope | {name: 'scope not set '}; if (! (_ Args instanceof Array) {_ args = ['$ scope', _ args];} if (typeof _ args [_ args. length-1]! = 'Function') {throw new Error ('function is not specified in the parameter ');} _ args. map (arg, index) =>{ if (typeof arg = 'string') {if (arg = '$ scope ') {_ args [index] = _ opts. scope;} else {if (!! Arg &&! (Arg in this. servicesList) {throw new Error ('plug-in: '+ arg +' not registered yet ');} _ args [index] = this. servicesList [arg] ;}}); return this. applicationList [this. applicationList. push ({run: function (callback) {if (typeof callback! = 'Function') {callback = function (_ opts) {return _ opts;} return callback (_ args [_ args. length-1]. apply (null, _ args) ;}})-1] ;}}; // framework end // The plug-in starts mqyJs. servicesRegister ('$ hello', {name: ''}); mqyJs. servicesRegister ('$ world', {name: 'World'}); mqyJs. servicesRegister ('$ China', {name: 'China'}); var app = mqyJs. applicationCreate (['$ scope', '$ hello',' $ China', function ($ scope, $ hello, $ china) {return $ scope. name + ":" + $ hello. name + $ china. name;}]); var app2 = mqyJs. applicationCreate ([{name: 'directly input SCOPE '},' $ hello', '$ world', function ($ scope, $ hello, $ world) {return $ scope. name + ":" + $ hello. name + $ world. name;}]); var app3 = mqyJs. applicationCreate ([{name: 'World is also passed in directly '},' $ hello', {name: 'global'}, function ($ scope, $ hello, $ world) {return $ scope. name + ":" + $ hello. name + $ world. name;}]); var app4 = mqyJs. applicationCreate (function ($ scope) {return $ scope. name ;}); var opts = {scope: {name: 'custom SCOPE '}}; var app5 = mqyJs. applicationCreate (opts, function ($ scope) {return $ scope. name;}); app4.run (function (result) {console. log ('manually call-> RESULT-> '+ result) ;}); // plug-in ends mqyJs. applicationList. map (function (app, index) {console. log ('automatic call-> APP # '+ index +'-> '+ app. run ());});

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.