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的$getjson调用并获取远程的JSON字符串问题
Dec 10 Javascript
javascript 利用Image对象实现的埋点(某处的点击数)统计
Dec 28 Javascript
JavaScript使用DeviceOne开发实战(四)仿优酷视频应用
Dec 02 Javascript
js实现图片轮播效果
Dec 19 Javascript
JS实现设置ff与ie元素绝对位置的方法
Mar 08 Javascript
JS实现淡入淡出图片效果的方法分析
Dec 20 Javascript
jquery实现超简单的瀑布流布局【推荐】
Mar 08 Javascript
微信小程序之前台循环数据绑定
Aug 18 Javascript
解决vue打包css文件中背景图片的路径问题
Sep 03 Javascript
Vue中对iframe实现keep alive无刷新的方法
Jul 23 Javascript
vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作
Jul 22 Javascript
Vue实现一种简单的无限循环滚动动画的示例
Jan 10 Vue.js
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图片处理之使用imagecopy函数添加图片水印实例
2014/11/19 PHP
PHP中的session安全吗?
2016/01/22 PHP
javascript Array.remove() 数组删除
2009/08/06 Javascript
用js做一个小游戏平台 (一)
2009/12/29 Javascript
jquery链式操作的正确使用方法
2014/01/06 Javascript
Javascript学习笔记之函数篇(六) : 作用域与命名空间
2014/11/23 Javascript
javascript实现简单的二级联动
2015/03/19 Javascript
学好js,这些js函数概念一定要知道【推荐】
2017/01/19 Javascript
JavaScript数组迭代方法
2017/03/03 Javascript
jquery点赞功能实现代码 点个赞吧!
2020/05/29 jQuery
js实现城市级联菜单的2种方法
2017/06/23 Javascript
微信小程序之页面跳转和参数传递的实现
2017/09/29 Javascript
nodejs微信扫码支付功能实现
2018/02/17 NodeJs
.vue文件 加scoped 样式不起作用的解决方法
2018/05/28 Javascript
angularJs中跳转到指定的锚点实例($anchorScroll)
2018/08/31 Javascript
详解Puppeteer前端自动化测试实践
2019/02/21 Javascript
JavaScript常用工具函数库汇总
2020/09/17 Javascript
Python计时相关操作详解【time,datetime】
2017/05/26 Python
Python实现简易版的Web服务器(推荐)
2018/01/29 Python
tensorflow实现简单的卷积网络
2018/05/24 Python
python 移除字符串尾部的数字方法
2018/07/17 Python
详解Python绘图Turtle库
2019/10/12 Python
Django实现WebSSH操作物理机或虚拟机的方法
2019/11/06 Python
Python3中configparser模块读写ini文件并解析配置的用法详解
2020/02/18 Python
Python使用Matlab命令过程解析
2020/06/04 Python
苹果中国官方网站:Apple中国
2016/07/22 全球购物
JD Sports德国官网:英国领先的运动鞋和运动服饰零售商
2018/02/26 全球购物
公司成立感言
2014/01/11 职场文书
个人租房协议书
2014/04/09 职场文书
小学学雷锋活动总结
2014/04/25 职场文书
镇党政领导班子民主生活会思想汇报
2014/10/11 职场文书
2015年实习单位评语
2015/03/25 职场文书
2015民办小学年度工作总结
2015/05/26 职场文书
安全生产标语口号
2015/12/26 职场文书
Golang gRPC HTTP协议转换示例
2022/06/16 Golang
输入框跟随文字内容适配宽实现示例
2022/08/14 Javascript