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在光标位置插入内容的实现代码
Jun 18 Javascript
当滚动条滚动到页面底部自动加载增加内容的js代码
May 13 Javascript
Node.js 的异步 IO 性能探讨
Oct 08 Javascript
2种jQuery 实现刮刮卡效果
Feb 01 Javascript
轻松实现js图片预览功能
Jan 18 Javascript
js实现统计字符串中特定字符出现个数的方法
Aug 02 Javascript
JS中script标签defer和async属性的区别详解
Aug 12 Javascript
基于javascript实现最简单选项卡切换
Feb 01 Javascript
通过vue提供的keep-alive减少对服务器的请求次数
Apr 01 Javascript
详解webpack-dev-server 设置反向代理解决跨域问题
Apr 18 Javascript
Vue keepAlive 数据缓存工具实现返回上一个页面浏览的位置
May 10 Javascript
linux服务器快速卸载安装node环境(简单上手)
Feb 22 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
第十三节--对象串行化
2006/11/16 PHP
PHP 开源AJAX框架14种
2009/08/24 PHP
php自定义函数之递归删除文件及目录
2010/08/08 PHP
利用php下载xls文件(自己动手写的)
2014/04/18 PHP
PHP 魔术变量和魔术函数详解
2015/02/25 PHP
PHP IDE phpstorm 常用快捷键
2015/05/18 PHP
PHP实现获取文件后缀名的几种常用方法
2015/08/08 PHP
4种PHP异步执行的常用方式
2015/12/24 PHP
PHP实现的方程求解示例分析
2016/11/11 PHP
点图片上一页下一页翻页效果
2008/07/09 Javascript
js实现倒计时(距离结束还有)示例代码
2013/07/24 Javascript
jquery数组之存放checkbox全选值示例代码
2013/12/20 Javascript
js 左右悬浮对联广告特效代码
2014/12/12 Javascript
jQuery-1.9.1源码分析系列(十)事件系统之事件体系结构
2015/11/19 Javascript
全面解析Bootstrap中tab(选项卡)的使用方法
2016/06/06 Javascript
javascript深拷贝和浅拷贝详解
2017/02/14 Javascript
详解基于Vue+Koa的pm2配置
2017/10/24 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
解决Vue 项目打包后favicon无法正常显示的问题
2018/09/01 Javascript
详解基于mpvue微信小程序下载远程图片到本地解决思路
2019/05/16 Javascript
Electron 调用命令行(cmd)
2019/09/23 Javascript
Bootstrap table 服务器端分页功能实现方法示例
2020/06/01 Javascript
微信小程序实现列表滚动头部吸顶的示例代码
2020/07/12 Javascript
[01:31:03]DOTA2完美盛典全回顾 见证十五项大奖花落谁家
2017/11/28 DOTA
Python中的单继承与多继承实例分析
2018/05/10 Python
pytorch 图像预处理之减去均值,除以方差的实例
2020/01/02 Python
Python namedtuple命名元组实现过程解析
2020/01/08 Python
PyTorch: Softmax多分类实战操作
2020/07/07 Python
python 爬取英雄联盟皮肤并下载的示例
2020/12/04 Python
HTML5和CSS3让网页设计提升到下一个高度
2009/08/14 HTML / CSS
css3 盒模型以及box-sizing属性全面了解
2016/09/20 HTML / CSS
Mio Skincare英国官网:身体紧致及孕期身体护理
2018/08/19 全球购物
运动会宣传语
2015/07/13 职场文书
django学习之ajax post传参的2种格式实例
2021/05/14 Python
分享MySQL常用 内核 Debug 几种常见方法
2022/03/17 MySQL
Springboot中如何自动转JSON输出
2022/06/16 Java/Android