今天在读 jQuery代码的时候,发现下面的写法:
1
2
3
|
( function ( window, undefined ) {
...
})(window);
|
window 肯定是没问题, 表示 BOM 浏览器对象模型中的 window 对象。但是这里为什么会有一个名为 undefined 的形参呢?起初的时候很不理解。去技术群请教了一下,才真正理解了这里的原因。
原来,Javascript 中的 undefined 并不是作为关键字(全部Javascript关键字列表)出现的。因此可以允许用户对其赋值。例如:
1
|
var undefined = 'myValue' ;
|
如此一来,假如 jQuery 中使用下面的写法:
1
2
3
|
( function ( window ) {
...
})(window);
|
必然造成中间代码里的 undefined 遭到污染。因为在默认情况下,对于一个未定义的变量,它的值应该是 undefined,假如用户使用形如
1
2
3
4
|
var undefined = 'myValue' ;
window.undefined = 'myValue' ;
|
的代码进行赋值,那么,jQuery 中的 undefined 的值就变成了用户指定的值(这里是字符串 ‘myValue’)。这样会造成 jQuery 内部异常。
而 jQuery 采用的这种写法,就很好的避免了这个问题。在执行匿名函数的时候,只传递一个参数 window, 而不传递 undefined,那么函数体中的 undefined 局部变量的值,刚好就是 undefined. 甚为巧妙啊。
-------------------------------------------------------------------------------------------------------------------------------------------------------
Use to add a single line comment, or comment out a single line of code.
Use to add a multi-line comment, or comment out multiple lines of code.
Adds two values together or concatenates two strings into a single string.
Subtracts the value of a number from another number.
Multiples the values of two numbers.
Divides a number by another number.
Divides a number by another number and returns the remainder.
Increments the value of a number by 1.
Decrements the value of a number by 1.
Changes the sign of a signed integer.
Assigns a value to a variable or other object.
Adds the value of the first item to the second item and assigns the total to the first item as a new value.
Subtracts the value of the second item from the first item and assigns the total to the first item as a new value.
Multiples the value of the first item by the second item and assigns the total to the first item as a new value.
Divides the value of the first item by the second item and assigns the total to the first item as a new value.
Shifts the first item in binary representation the value of the second item of bits to the right, discarding bits shifted off, and assigns the new value to the first item.
font-weight: bold; font-size: 13px; color: #8f8781; margin-bottom: 0.5em; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-colo
分享到:
相关推荐
jquery点击弹窗2种写法
jquery弹出窗口,还是很好用的。可以在项目中用来做提示框。
通过Jquery弹出window窗体,窗体中可以添加一系列的操作,在项目中应用范围很广,稍加修改就能应用到项目中。
JQuery插件的写法 最常用的jquery插件开发方式(教程合集) jquery插件的例子 jQuery插件开发教程_看这一本就行了 JQuery插件写法 jq插件最常用的写法
;(function($){$.extend($.fn… ...3、因为undefined是window的属性,声明为局部变量之后,在函数中如果再有变量与undefined作比较的话,程序就可以不用搜索undefined到window,可以提高程序性能。
Jquery ready function Tester Source!
jquery alert 信息提示 精通JavaScript+jQuery
页面前端 jquery 用法 (function($) {})(jQuery);的用法
JQuery DIV 实现window.confirm美化确认提示框
jQuery链式写法:实现点击标签展示列表页及标签样式及内容,再次点击收起列表页还原标签样式及内容!
jquery.DOMWindow弹出层与TAB切换实例汇总,jquery.DOMWindow.js是浮动弹出框的核心部件,本插件的弹出框有多种形式,比如它可以弹出不带边框的、带有淡入淡出特效的、各种颜色的背景浮动框、弹出后背景会变暗的浮动...
主要介绍了JQuery调用绑定click事件的3种写法,本文简洁清晰的给出3种写法的代码示例,可以很方便的复制使用,需要的朋友可以参考下
jQuery的function函数中使用外部变量: //如何取得i的变量 for(i=0;i<3;i++) { $.get("/test.html", function(data){ alert(i) }); } //使用闭包: for(i=0;i<3;i++) { (function(index){ $....
本文通过具体示例向大家详细介绍了jQuery中的(function($){})(jQuery)的用法和意义,对此有相同困惑的小伙伴可以参考下本文。
jQuery(zepto) 插件,把各种延迟串联起来,采用管道式写法 — 为了少写几个 setTimeout
本示例主要用于揭示Jquery中的(function($){...})(jQuery),欢迎下载!!!相关博客:http://blog.csdn.net/gaohuanjie/article/details/38268831
jQuery API 1.4.4 中文参考手册.chm,jquery api 下载,jQuery 1.4.4 API 参考文档中文版.rar,jQuery最新版CHM下载,jQuery中文参考手册,jQuery中文版1.4.4参考手册,jQuery 1.4.4 速查表,jQuery中文参考手册,jQuery中文...
修复 jQuery 中 isFunction 方法的 BUG
jQueryPrj3_入门BaseDao的写法 本文来自: 俏讯网盘(http://download.dehome.net) 详细出处参考:http://download.dehome.net/viewfile.php?file_id=6