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 相关文章推荐
AJAX架构之Dojo篇
Apr 10 Javascript
整理一些JavaScript的IE和火狐的兼容性注意事项
Mar 17 Javascript
自定义的一个简单时尚js下拉选择框
Nov 20 Javascript
jqplot通过ajax动态画折线图的方法及思路
Dec 08 Javascript
jquery遍历筛选数组的几种方法和遍历解析json对象
Dec 13 Javascript
理解javascript中的原型和原型链
Jul 30 Javascript
不依赖Flash和任何JS库实现文本复制与剪切附源码下载
Oct 09 Javascript
AngularJs Dependency Injection(DI,依赖注入)
Sep 02 Javascript
jquery实现全选、不选、反选的两种方法
Sep 06 Javascript
JavaScript模块化之使用requireJS按需加载
Apr 12 Javascript
微信小程序开发之数据存储 参数传递 数据缓存
Apr 13 Javascript
elementUI Tree 树形控件的官方使用文档
Apr 25 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/10/09 PHP
PHP4实际应用经验篇(7)
2006/10/09 PHP
使用php+Ajax实现唯一校验实现代码[简单应用]
2011/11/29 PHP
日常整理PHP中简单的图形处理(经典)
2015/10/26 PHP
php获取远程文件内容的函数
2015/11/02 PHP
PHP与Ajax相结合实现登录验证小Demo
2016/03/16 PHP
载入jQuery库的最佳方法详细说明及实现代码
2012/12/28 Javascript
jquery easyui滚动条部分设置介绍
2013/09/12 Javascript
jquery delay()介绍及使用指南
2014/09/02 Javascript
JavaScript中的逻辑判断符&amp;&amp;、||与!介绍
2014/12/31 Javascript
jquery实现页面关键词高亮显示的方法
2015/03/12 Javascript
浅析AngularJs HTTP响应拦截器
2015/12/28 Javascript
搭建Bootstrap离线文档的方法
2016/12/02 Javascript
js仿微博动态栏功能
2017/02/22 Javascript
AngularJS动态绑定ng-options的ng-model实例代码
2017/06/21 Javascript
解决vue里碰到 $refs 的问题的方法
2017/07/13 Javascript
bootstrap datepicker插件默认英文修改为中文
2017/07/28 Javascript
Vue.js框架路由使用方法实例详解
2017/08/25 Javascript
基于Bootstrap实现城市三级联动
2017/11/23 Javascript
详解Vue中使用Echarts的两种方式
2018/07/03 Javascript
详解ng-alain动态表单SF表单项设置必填和正则校验
2019/06/11 Javascript
Vue实现购物车实例代码两则
2020/05/30 Javascript
详谈Pandas中iloc和loc以及ix的区别
2018/06/08 Python
Django ManyToManyField 跨越中间表查询的方法
2018/12/18 Python
python 模拟贷款卡号生成规则过程解析
2019/08/30 Python
python多线程高级锁condition简单用法示例
2019/11/07 Python
python matplotlib拟合直线的实现
2019/11/19 Python
python3 使用Opencv打开USB摄像头,配置1080P分辨率的操作
2019/12/11 Python
numpy库ndarray多维数组的维度变换方法(reshape、resize、swapaxes、flatten)
2020/04/28 Python
html5 Canvas实现图片旋转的示例
2018/01/15 HTML / CSS
Deichmann英国:德国鞋类零售商
2021/01/30 全球购物
幼儿园大班开学寄语
2014/08/02 职场文书
2015年乡镇卫生院妇幼保健工作总结
2015/05/19 职场文书
荒岛余生观后感
2015/06/09 职场文书
详解Python+OpenCV进行基础的图像操作
2022/02/15 Python
漫画《催眠麦克风-Dawn Of Divisions》第二卷PV公开
2022/04/05 日漫