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 相关文章推荐
jquery 表格分页等操作实现代码(pagedown,pageup)
Apr 11 Javascript
jquery 使用点滴函数代码
May 20 Javascript
js实现浮动在网页右侧的简洁QQ在线客服代码
Sep 04 Javascript
jquery的幻灯片图片切换效果代码分享
Sep 07 Javascript
Jquery Mobile 自定义按钮图标
Nov 18 Javascript
JS实现动态增加和删除li标签行的实例代码
Oct 16 Javascript
JavaScript闭包和范围实例详解
Dec 19 Javascript
详解vuelidate 对于vueJs2.0的验证解决方案
Mar 09 Javascript
深入浅析ES6 Class 中的 super 关键字
Oct 20 Javascript
Promise.all中对于reject的处理方法
Aug 01 Javascript
layui关闭弹窗后刷新主页面和当前更改项的例子
Sep 06 Javascript
使用vue制作滑动标签
Sep 21 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 Global定义全局变量使用说明
2013/08/15 PHP
利用yahoo汇率接口实现实时汇率转换示例 汇率转换器
2014/01/14 PHP
深入浅析php json 格式控制
2015/12/24 PHP
thinkPHP数据库增删改查操作方法实例详解
2016/12/06 PHP
php自定义排序uasort函数示例【二维数组按指定键值排序】
2019/06/19 PHP
Extjs中常用表单介绍与应用
2010/06/07 Javascript
基于jquery的一个浮动框(扩展性比较好 )
2010/08/27 Javascript
一些主流JS框架中DOMReady事件的实现小结
2011/02/12 Javascript
真正的JQuery.ajax传递中文参数的解决方法
2011/05/28 Javascript
js 关于=+与+=日期函数使用说明(赋值运算符)
2011/11/15 Javascript
jquery操作cookie插件分享
2014/01/14 Javascript
Javascript学习笔记之数组的遍历和 length 属性
2014/11/23 Javascript
jQuery插件PageSlide实现左右侧栏导航菜单
2015/04/12 Javascript
JQuery插件jcarousellite的参数中文说明
2015/05/11 Javascript
AngularJs动态加载模块和依赖注入详解
2016/01/11 Javascript
提高Web性能的前端优化技巧总结
2017/02/27 Javascript
nodejs和react实现即时通讯简易聊天室功能
2019/08/21 NodeJs
基于vue实现简易打地鼠游戏
2020/08/21 Javascript
[03:59]5分钟带你了解什么是DOTA2(第二期)
2017/02/07 DOTA
Numpy数组的保存与读取方法
2018/04/04 Python
对python制作自己的数据集实例讲解
2018/12/12 Python
pytorch加载自定义网络权重的实现
2020/01/07 Python
基于plt.title无法显示中文的快速解决
2020/05/16 Python
python爬虫如何解决图片验证码
2021/02/14 Python
世界上最好的帽子:Tilley
2016/11/27 全球购物
Skyscanner波兰:廉价航班
2017/11/07 全球购物
上海天奕面试题笔试题
2015/04/19 面试题
校园文明倡议书
2014/05/16 职场文书
单位委托书范本(3篇)
2014/09/18 职场文书
基层党员干部四风问题整改方向和措施
2014/09/25 职场文书
在职人员跳槽求职信
2015/03/20 职场文书
2015年入党积极分子评语
2015/03/26 职场文书
2015年小学一年级班主任工作总结
2015/05/21 职场文书
劳动模范获奖感言
2015/07/31 职场文书
带你学习MySQL执行计划
2021/05/31 MySQL
MySQL基础快速入门知识总结(附思维导图)
2021/09/25 MySQL