[JavaScript] self-executed functions and javascript-executed Functions

Source: Internet
Author: User

[JavaScript] self-executed functions and javascript-executed Functions

Recently, when I was in contact with mui, I encountered a piece of code:

1 (function ($, doc) {2 $. init ({3 statusBarBackground: '# f7f7f7' 4}); 5 $. plusReady (function () {6 plus. screen. lockOrientation ("portrait-primary"); 7 var settings = app. getSettings (); 8 var state = app. getState (); 9 var mainPage = $. preload ({10 "id": 'main', 11 "url": 'tab-webview-main.html '12}); 13 var main_loaded_flag = false; 14 mainPage. addEventListener ("loaded", function () {15 main_loaded_flag = true; 16}); 17 var toMain = function () {18 // reasons for using the Timer: 19 // execution may be too fast. When the loaded event on the main page is not triggered, the Custom Event will be executed. At this time, the failure will inevitably fail 20 var id = setInterval (function () {21 if (main_loaded_flag) {22 clearInterval (id); 23 $. fire (mainPage, 'show', null); 24 mainPage. show ("pop-in"); 25} 26}, 20); 27}; 28 29 // close splash 30 setTimeout (function () {31 // disable splash 32 plus. navigator. closeSplashscreen (); 33}, 600); 34 var loginButton = doc. getElementById ('login'); 35 var accountBox = doc. getElementById ('account'); 36 var passwordBox = doc. getElementById ('Password'); 37 var autoLoginButton = doc. getElementById ("autoLogin"); 38 var regButton = doc. getElementById ('reg '); 39 var forgetButton = doc. getElementById ('forgetpassword'); 40 loginButton. addEventListener ('tap', function (event) {41 var username = $ ('# account '). val (); 42 var password =$ ('# password '). val (); 43 44 // LeanCloud-Logon 45 // https://leancloud.cn/docs/leanstorage_guide-js.html# Log On with the user name and password 46 AV. user. logIn (username, password ). then (function (loginedUser) {47 toMain (); 48}, function (error) {49 // import AlertDialog class 50 var AlertDialog = plus. android. importClass ("android. app. alertDialog "); 51 // create a prompt box to construct the object. The constructor must provide the global environment object of the program. android. runtimeMainActivity () method to obtain 52 var dlg = new AlertDialog. builder (plus. android. runtimeMainActivity (); 53 // set the title of the prompt box 54 dlg. setTitle ("Logon Failed"); 55 // Set the prompt box content 56 dlg. setMessage ("the user name or password is incorrect! "+ JSON. stringify (error); 57 // set the prompt box button to 58 dlg. setPositiveButton ("OK", null); 59 // The prompt box is 60 dlg. show (); 61}); 62}); 63 $. enterfocus ('# login-form input', function () {64 $. trigger (loginButton, 'tap'); 65}); 66 autoLoginButton. classList [settings. autoLogin? 'Add': 'delete'] ('mui-active') 67 autoLoginButton. addEventListener ('toggle ', function (event) {68 setTimeout (function () {69 var isActive = event. detail. isActive; 70 settings. autoLogin = isActive; 71 app. setSettings (settings); 72}, 50); 73}, false); 74 regButton. addEventListener ('tap', function (event) {75 $. openWindow ({76 url: 'reg.html ', 77 id: 'reg', 78 preload: true, 79 show: {80 aniShow: 'Pop-in '81}, 82 styles: {83 popgsture: 'hide '84}, 85 waiting: {86 autoShow: false 87} 88}); 89 }, false); 90 forgetButton. addEventListener ('tap', function (event) {91 $. openWindow ({92 url: 'forget_password.html ', 93 id: 'forget _ password', 94 preload: true, 95 show: {96 aniShow: 'pop-in' 97 }, 98 styles: {99 popGesture: 'hide '100}, 101 waiting: {102 autoShow: false103} 104}); 105}, fa Lse); 106 // 107 window. addEventListener ('resize', function () {108 oauthArea. style. display = document. body. clientHeight> 400? 'Block': 'none'; 109}, false); 110 // 111 var backButtonPress = 0; 112 $. back = function (event) {113 backButtonPress ++; 114 if (backButtonPress> 1) {115 plus. runtime. quit (); 116} else {117 plus. nativeUI. toast ('exit application once again '); 118} 119 setTimeout (function () {120 backButtonPress = 0; 121}, 1000); 122 return false; 123 }; 124); 125} (mui, document ));Mui

I don't know why a function should be placed in a bracket, and there is a bracket (mui, document) in the brackets, which looks like a parameter. I thought this was the content in Native. js. Then I went to the official document and looked at the content of Njs. no, I went to Baidu.

Self-executed Functions

Self-executed function (jquery) {}( jquery ));
* It is equivalent to the following code:

var fun = function(jquery){};fun(jquery);

* Immediately execute the anonymous function fun (jquery) to avoid conflicts between jquery and other class libraries or variables.

There are many jquery words in Baidu, but they are not clearly related to jquery, and I am not quite clear about it.

In the mui demo (function ($, doc) {} (mui, document); it is said that the function can be executed after the document is loaded, equivalent

$ (Document). ready (function () {// function body });

 

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.