javascript全局变量封装模块实现代码


Posted in Javascript onNovember 28, 2012

下面的代码是我的测试代码,注释很重要:

/*global window,jQuery,validate_email,masterUI,$,rest*/ 
/** Enable ECMAScript "strict" operation for this function. See more: 
* http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ 
* http://stackoverflow.com/questions/5020479/what-advantages-does-using-functionwindow-document-undefined-windo 
* Q1: Why are window and document being fed instead of just being accessed normally? 
* A1: Generally to fasten the identifier resolution process, having them as local variables can help (although IMO the performance improvements may be negligible). 
* A2: Passing the global object is also a widely used technique on non-browser environments, where you don't have a window identifier at the global scope, e.g.: 
* (function (global) { 
* //.. 
* })(this); // this on the global execution context is the global object itself 
* A3: Passing window and document allows the script to be more efficiently minified 
* 
* Q2: Why the heck is undefined being passed in? 
* A1: This is made because the undefined global property in ECMAScript 3, is mutable, meaning that someone could change its value affecting your code, for example: 
* undefined = true; // mutable 
* (function (undefined) { 
* alert(typeof undefined); // "undefined", the local identifier 
* })(); // <-- no value passed, undefined by default 
* If you look carefully undefined is actually not being passed (there's no argument on the function call), 
* that's one of the reliable ways to get the undefined value, without using the property window.undefined. 
* 
*/ 
(function(window, document, undefined) { 
"use strict"; 
window.test = { 
init: function () { 
"use strict"; 
alert("ok"); 
} 
}; 
})(window, document);// no undefined parameter here to avoid using mutable window.undefined changed by other guy

1.说明,参考了一篇文章和stackoverflow上的一个帖子
2.(function(){})() 这种代码写在独立的js文件里,当js文件被html加载的时候,该函数就会执行。实际上创建了windows.text对象。
以后html代码就可用test.init的形式调用方法。
测试html部分代码如下:
[plain] view plaincopyprint? 
<head> 
<title>AppEngine SDK</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<script type="text/javascript" src="../../master/script/third_party/jquery-1.8.0.min.js"></script> 
<script type="text/javascript" src="../../master/plugin/jquery-validation-1.9.0/jquery.validate.js"></script> 
<script type="text/javascript" src="../../master/plugin/artDialog4.1.6/jquery.artDialog.js"></script> 
<script type="text/javascript" src="../../master/script/app/test.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
test.init(); 
}) 
</script> 
</head>

3.Jslint会报两个问题,一是关于undefined的,没找到什么好方法,任它抱怨吧。另一格式最后调用方式要改成:
[javascript] view plaincopyprint?}(window, document)); }(window, document));

无所谓了,就任由它吧。只要功能正常就行。
Javascript 相关文章推荐
js网页中的(运行代码)功能实现思路
Feb 04 Javascript
ExtJS DOM元素操作经验分享
Aug 28 Javascript
jquery实现瀑布流效果分享
Mar 26 Javascript
MVC Ajax Helper或Jquery异步加载部分视图
Nov 29 Javascript
分享我的jquery实现下拉菜单心的
Nov 29 Javascript
jquery动态添加带有样式的HTML标签元素方法
Feb 24 jQuery
jQuery实现使用sort方法对json数据排序的方法
Apr 17 jQuery
Vue 父子组件数据传递的四种方式( inheritAttrs + $attrs + $listeners)
May 04 Javascript
在小程序中使用腾讯视频插件播放教程视频的方法
Jul 10 Javascript
对node通过fs模块判断文件是否是文件夹的实例讲解
Jun 10 Javascript
vue中使用element组件时事件想要传递其他参数的问题
Sep 18 Javascript
npm全局环境变量配置详解
Dec 15 Javascript
Javascript Request获取请求参数如何实现
Nov 28 #Javascript
js移除事件 js绑定事件实例应用
Nov 28 #Javascript
js arguments对象应用介绍
Nov 28 #Javascript
web基于浏览器的本地存储方法应用
Nov 27 #Javascript
extjs 04_grid 单击事件新发现
Nov 27 #Javascript
javascript 正则表达式相关应介绍
Nov 27 #Javascript
javascript 二进制运算技巧解析
Nov 27 #Javascript
You might like
php adodb分页实现代码
2009/03/19 PHP
php简单的留言板与回复功能具体实现
2014/02/19 PHP
详解PHP中strlen和mb_strlen函数的区别
2014/03/07 PHP
VB中的RasEnumConnections函数返回632错误解决方法
2014/07/29 PHP
thinkphp3.x自定义Action、Model及View的简单实现方法
2016/05/19 PHP
Mootools 1.2教程 正则表达式
2009/09/15 Javascript
javascript显示用户停留时间的简单实例
2013/08/05 Javascript
js漂浮广告实现代码
2015/08/15 Javascript
javascript 中的 delete及delete运算符
2015/11/15 Javascript
基于js实现微信发送好友如何分享到朋友圈、微博
2015/11/30 Javascript
浅谈String.valueOf()方法的使用
2016/06/06 Javascript
浅谈Sublime Text 3运行JavaScript控制台
2016/06/06 Javascript
微信小程序实现根据字母选择城市功能
2017/08/16 Javascript
AngularJS模态框模板ngDialog的使用详解
2018/05/11 Javascript
[46:47]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS NE
2014/05/22 DOTA
Python采用raw_input读取输入值的方法
2014/08/18 Python
使用Python开发windows GUI程序入门实例
2014/10/23 Python
Python使用turtule画五角星的方法
2015/07/09 Python
Python实现的计算马氏距离算法示例
2018/04/03 Python
python多线程同步之文件读写控制
2021/02/25 Python
Python解析json代码实例解析
2019/11/25 Python
Python实现图像去噪方式(中值去噪和均值去噪)
2019/12/18 Python
Python计算指定日期是今年的第几天(三种方法)
2020/03/26 Python
基于SQLAlchemy实现操作MySQL并执行原生sql语句
2020/06/10 Python
使用Keras预训练好的模型进行目标类别预测详解
2020/06/27 Python
使用keras时input_shape的维度表示问题说明
2020/06/29 Python
台湾租车首选品牌:IWS艾维士租车
2019/05/03 全球购物
教师实习自我鉴定
2013/12/18 职场文书
财务主管岗位职责
2014/02/28 职场文书
交通事故赔偿协议书
2014/04/15 职场文书
优秀会计求职信
2014/07/04 职场文书
委托书怎样写
2014/08/30 职场文书
2014年学生会工作总结范文
2014/11/07 职场文书
小学英语复习计划
2015/01/19 职场文书
公司客户答谢酒会祝酒词
2015/08/11 职场文书
如何自己动手写SQL执行引擎
2021/06/02 MySQL