[JavaScript]自執行函數

來源:互聯網
上載者:User

標籤:username   import   height   java   roi   sre   分享   logs   匿名   

最近在接觸mui的時候,遇到了一段代碼:

  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                         //使用定時器的原因: 19                         //可能執行太快,main頁面loaded事件尚未觸發就執行自訂事件,此時必然會失敗 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                         //關閉 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 - 登入 45                           // https://leancloud.cn/docs/leanstorage_guide-js.html#使用者名稱和密碼登入 46                           AV.User.logIn(username, password).then(function (loginedUser) { 47                                 toMain(); 48                           }, function (error) { 49                                 // 匯入AlertDialog類   50                             var AlertDialog = plus.android.importClass("android.app.AlertDialog");   51                                 // 建立提示框構造對象,建構函式需要提供者全域環境對象,通過plus.android.runtimeMainActivity()方法擷取   52                             var dlg = new AlertDialog.Builder(plus.android.runtimeMainActivity());   53                             // 設定提示框標題   54                             dlg.setTitle("登陸失敗");   55                             // 設定提示框內容   56                             dlg.setMessage("使用者名稱或密碼不正確!"+JSON.stringify(error));   57                             // 設定提示框按鈕   58                             dlg.setPositiveButton("OK",null);   59                             // 顯示提示框   60                             dlg.show();   61                           }); 62                     }); 63                     $.enterfocus(‘#login-form input‘, function() { 64                         $.trigger(loginButton, ‘tap‘); 65                     }); 66                     autoLoginButton.classList[settings.autoLogin ? ‘add‘ : ‘remove‘](‘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                                 popGesture: ‘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                     }, false);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(‘再按一次退出應用‘);118                         }119                         setTimeout(function() {120                             backButtonPress = 0;121                         }, 1000);122                         return false;123                     };124                 });125             }(mui, document));
mui

這就不懂了,為什麼一個function函數要放在一個括弧裡面,括弧裡面還有一個括弧(mui,document),看起來像是參數一樣。還以為這是Native.js裡面的內容,然後去官方文檔中翻看了一下Njs的內容,並不是啊,然後去百度。

自執行函數

自執行函數(function(jquery){}(jquery));
* 相當於下面的代碼:

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

* 立即執行匿名函數fun(jquery);主要是為了避免jquery與其他類庫或變數有衝突。

百度中有好多jquery字樣出現,但是並沒有明確說跟jquery有關係,我也是不太清楚。

mui的demo中(function($, doc) {}(mui, document));據說可以讓函數在文檔載入完畢之後再執行,相當於

$(document).ready(function (){    //函數體});

 

[JavaScript]自執行函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.