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 相关文章推荐
Js动态创建div
Sep 25 Javascript
基于jQuery的可用于选项卡及幻灯的切换插件
Mar 28 Javascript
Prototype源码浅析 Number部分
Jan 16 Javascript
js取整数、取余数的方法
May 11 Javascript
简介JavaScript中Math.cos()余弦方法的使用
Jun 15 Javascript
深入理解js generator数据类型
Aug 16 Javascript
Node.js和Express简单入门介绍
Mar 24 Javascript
JavaScript创建对象_动力节点Java学院整理
Jun 27 Javascript
React Native 通告消息竖向轮播组件的封装
Aug 25 Javascript
jQuery实现鼠标点击处心形漂浮的炫酷效果示例
Apr 12 jQuery
Vue父子组件之间的通信实例详解
Sep 28 Javascript
layer页面跳转,获取html子节点元素的值方法
Sep 27 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
Terran热键控制
2020/03/14 星际争霸
便携利器 — TECSUN PL-365简评
2021/03/02 无线电
PHP 程序员的调试技术小结
2009/11/15 PHP
php的一个简单加密解密代码
2014/01/14 PHP
destoon后台网站设置变成空白的解决方法
2014/06/21 PHP
WordPress后台中实现图片上传功能的实例讲解
2016/01/11 PHP
PHP递归实现快速排序的方法示例
2017/12/18 PHP
javascript jQuery $.post $.ajax用法
2008/07/09 Javascript
jQuery Selector选择器小结
2010/05/06 Javascript
javascript用函数实现对象的方法
2015/05/14 Javascript
jQuery实现单击弹出Div层窗口效果(可关闭可拖动)
2015/09/19 Javascript
js中数组结合字符串实现查找(屏蔽广告判断url等)
2016/03/30 Javascript
layui-laydate时间日历控件使用方法详解
2018/11/15 Javascript
js中的this的指向问题详解
2019/08/29 Javascript
vue element实现表格合并行数据
2020/11/30 Vue.js
用vue设计一个日历表
2020/12/03 Vue.js
python有证书的加密解密实现方法
2014/11/19 Python
python实现在windows下操作word的方法
2015/04/28 Python
实例Python处理XML文件的方法
2015/08/31 Python
python TCP Socket的粘包和分包的处理详解
2018/02/09 Python
pandas表连接 索引上的合并方法
2018/06/08 Python
CentOS下Python3的安装及创建虚拟环境的方法
2018/11/28 Python
详解Python3中ceil()函数用法
2019/02/19 Python
Django分页功能的实现代码详解
2019/07/29 Python
Tensorflow 多线程与多进程数据加载实例
2020/02/05 Python
基于Python获取docx/doc文件内容代码解析
2020/02/17 Python
python 利用jieba.analyse进行 关键词提取
2020/12/17 Python
详解Html5微信支付爬坑之路
2018/07/24 HTML / CSS
使用html5 canvas绘制圆环动效
2019/06/03 HTML / CSS
计算机专业应届毕业生自荐信
2013/09/26 职场文书
升职自荐信
2013/11/28 职场文书
杠杆的科学教学反思
2014/01/10 职场文书
护士自荐信范文
2015/03/25 职场文书
入党团支部推荐意见
2015/06/02 职场文书
帝企鹅日记观后感
2015/06/10 职场文书
Python还能这么玩之用Python做个小游戏的外挂
2021/06/04 Python