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 相关文章推荐
解决使用attachEvent函数时,this指向被绑定的元素的问题的方法
Aug 13 Javascript
JQery 渐变图片导航效果代码 漂亮
Jan 01 Javascript
js 判断计算字符串长度/判断空的简单方法
Aug 05 Javascript
用unescape反编码得出汉字示例
Apr 24 Javascript
jQuery插件zoom实现图片全屏放大弹出层特效
Apr 15 Javascript
jQuery获取及设置表单input各种类型值的方法小结
May 24 Javascript
Node.js环境下JavaScript实现单链表与双链表结构
Jun 12 Javascript
每日十条JavaScript经验技巧(一)
Jun 23 Javascript
微信小程序 教程之注册页面
Oct 17 Javascript
详解Vue中使用Echarts的两种方式
Jul 03 Javascript
详解Vue取消eslint语法限制
Aug 04 Javascript
vue element中axios下载文件(后端Python)
May 10 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多线程批量采集下载美女图片的实现代码(续)
2013/06/03 PHP
thinkphp查询,3.X 5.0方法(亲试可行)
2017/06/17 PHP
使用jQuery轻松实现Ajax的实例代码
2010/08/16 Javascript
js判断鼠标左、中、右键哪个被点击的方法
2015/01/27 Javascript
jQuery显示和隐藏 常用的状态判断方法
2015/01/29 Javascript
浅谈jquery.fn.extend与jquery.extend区别
2015/07/13 Javascript
jquery实现滑动特效代码
2015/08/10 Javascript
JS+CSS实现TreeMenu二级树形菜单完整实例
2015/09/18 Javascript
使用Web Uploader实现多文件上传
2016/06/08 Javascript
Javascript Event(事件)的传播与冒泡
2017/01/23 Javascript
详解vue-resource promise兼容性问题
2017/06/20 Javascript
vue中路由参数传递可能会遇到的坑
2017/12/07 Javascript
微信小程序网络请求封装示例
2018/07/24 Javascript
如何在 JavaScript 中更好地利用数组
2018/09/27 Javascript
微信小程序非跳转式组件授权登录的方法示例
2019/05/22 Javascript
python模块简介之有序字典(OrderedDict)
2016/12/01 Python
Python3中lambda表达式与函数式编程讲解
2019/01/14 Python
在Pycharm terminal中字体大小设置的方法
2019/01/16 Python
Python利用heapq实现一个优先级队列的方法
2019/02/03 Python
基于Python实现视频的人脸融合功能
2020/06/12 Python
python 实时调取摄像头的示例代码
2020/11/25 Python
HTML5地理定位与第三方工具百度地图的应用
2016/11/17 HTML / CSS
html5默认气泡修改的代码详解
2020/03/13 HTML / CSS
斯洛伐克电子产品购物网站:DATART
2020/04/05 全球购物
班组安全员工作职责
2014/02/01 职场文书
代理班主任的自我评价
2014/02/04 职场文书
工程管理英文求职信
2014/03/18 职场文书
5.12护士节演讲稿
2014/04/30 职场文书
省级优秀班集体申报材料
2014/05/25 职场文书
淘宝店策划方案
2014/06/07 职场文书
关于九一八事变的演讲稿2014
2014/09/17 职场文书
2014年监理工作总结范文
2014/11/17 职场文书
2014年管理人员工作总结
2014/12/01 职场文书
祝寿主持词
2015/07/02 职场文书
用Python的绘图库(matplotlib)绘制小波能量谱
2021/04/17 Python
聊聊CSS粘性定位sticky案例解析
2022/06/01 HTML / CSS