javascript全局变量封装模块实现代码

 更新时间:2012年11月28日 11:19:27   作者:  
javascript全局变量封装模块的应用,本文将详细介绍,需要了解更多的朋友可以参考下
下面的代码是我的测试代码,注释很重要:
复制代码 代码如下:

/*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 函数式编程的原理

    JavaScript 函数式编程的原理

    要了解JavaScript中的函数式编程原理,必须理解一下两个知识点
    2009-10-10
  • JavaScript修改注册表实例代码

    JavaScript修改注册表实例代码

    这篇文章主要介绍了JavaScript修改注册表实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • 使用Javascript监控前端相关数据的代码

    使用Javascript监控前端相关数据的代码

    本篇文章详细的介绍了使用Javascript监控前端相关数据,可以及时的监控前端的错误,加载时间等,有需要的可以了解一下。
    2016-10-10
  • js控制div弹出层实现方法

    js控制div弹出层实现方法

    这篇文章主要介绍了js控制div弹出层实现方法,可实现点击链接弹出div浮动层,且背景色变暗的效果,是一款非常实用的特效源码,需要的朋友可以参考下
    2015-05-05
  • js实现网页图片轮换播放

    js实现网页图片轮换播放

    这篇文章主要为大家详细介绍了js实现网页图片轮换播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • JS实现统计字符串中字符出现个数及最大个数功能示例

    JS实现统计字符串中字符出现个数及最大个数功能示例

    这篇文章主要介绍了JS实现统计字符串中字符出现个数及最大个数功能,结合实例形式分析了javascript字符串遍历、统计相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • BootStrap Table对前台页面表格的支持实例讲解

    BootStrap Table对前台页面表格的支持实例讲解

    bootstrap-table是在bootstrap的基础上面做了一些封装,所以在使用bootstrap-table之前要导入的js和css,下面通过本文给大家详细介绍需要引入的文件,对bootstrap table 表格感兴趣的朋友一起看看吧
    2016-12-12
  • JavaScript前后端数据交互工具ajax使用教程

    JavaScript前后端数据交互工具ajax使用教程

    Ajax(Asynchronous Javascript And XML),即是异步的JavaScript和XML,Ajax其实就是浏览器与服务器之间的一种异步通信方式
    2022-10-10
  • 了解Javascript的模块化开发

    了解Javascript的模块化开发

    这篇文章主要介绍了了解Javascript的模块化开发,本文讲解了为什么需要模块化开发,模块化开发的形成原因等内容,需要的朋友可以参考下
    2015-03-03
  • js中less常用的方法小结

    js中less常用的方法小结

    下面小编就为大家带来一篇js中less常用的方法小结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08

最新评论