摘要:本篇教程介绍了JavaScript基础教程 自执行函数详解,希望阅读本篇文章以后大家有所收获,帮助大家对JavaScript的理解更加深入。
本篇教程介绍了JavaScript基础教程 自执行函数详解,希望阅读本篇文章以后大家有所收获,帮助大家对JavaScript的理解更加深入。
<
最近在接触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: false
103 }
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 (){
//函数体
});
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标WEB前端JavaScript频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号