下面的代码是我的测试代码,注释很重要:
/*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部分代码如下:
<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的,没找到什么好方法,任它抱怨吧。另一格式最后调用方式要改成:
}(window, document));
无所谓了,就任由它吧。只要功能正常就行。
最后推荐一篇文章,前面的基础部分非常清晰的描述了anonymous closures 的使用。搞JavaScript的朋友看看会很有帮助。
http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
分享到:
相关推荐
该工具类为一个使用js 闭包封装的一个提示模态框,使用于不同分辨率的设备包括pc
深入理解javascript原型和闭包(01)——一切都是对象 深入理解javascript原型和闭包(02)——函数和对象的关系
本文结合 ECMA 262 规范详解了闭包的内部工作机制,让 JavaScript 编程人员对闭包的理解从“嵌套的函数”深入到“标识符解析、执行环境和作用域链”等等 JavaScript 对象背后的运行机制当中,真正领会到闭包的实质。
javascript原型和闭包
JavaScript闭包 JavaScript闭包 JavaScript闭包 JavaScript闭包
匿名函数,也称为拉姆达函数,是一种使用JavaScript函数的强大方式。以下总结了匿名函数的特点: 任何函数表达式从技术上说都是匿名函数,因为没有引用它们的确定的方式; 在无法确定如何引用函数的情况下,递归...
本文主要介绍了JavaScript利用闭包实现模块化的方法。具有一定的参考价值,下面跟着小编一起来看下吧
前端面试题,包含JavaScript的闭包,作用域,原型,原型链,上下文环境以及DOM,BOM封装函数深度克隆,以及一些常见的·JS问题,试题简单但是容易混淆,作为前端工程师必考题
javascript里的闭包是什么 什么是闭包.zip
闭包是ECMAScript (JavaScript)最强大的特性之一,但用好闭包的前提是必须理解闭包。闭包的创建相对容易,人们甚至会在不经意间创建闭包,但这些无意创建的闭包却存在潜在的危害,尤其是在比较常见的浏览器环境下...
Javascript中有几个非常...其中闭包 对于那些使用传统静态语言C/C++的程序员来说是一个新的语言特性。本文将以例子入手来介绍Javascript闭包的语言特性,并结合一点 ECMAScript语言规范来使读者可以更深入的理解闭包。
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现。
在复杂的逻辑下, JavaScript 需要被模块化,模块需要封装起来,只留下供外界调用的接口。闭包是 JavaScript 中实现模块封装的关键,也是很多初学者难以理解的要点
javascript笔记之匿名函数和闭包.docx
浅谈JavaScript for循环 闭包_.docx
Javascript 闭包完整解释
面向对象概念(对象封装,各种继承,闭包原理,this作用域等)介绍清晰易懂
在咱们的第一节(深入理解javascript原型和闭包(1)——一切都是对象)中说道,函数也是一种对象。他也是属性的集合,你也可以对函数进行自定义属性。不用等咱