`
hlbng
  • 浏览: 176730 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

关于 jQuery中 function( window, undefined ) 写法的原因讨论

阅读更多

今天在读 jQuery代码的时候,发现下面的写法:

1
2
3
(function( window, undefined ) {
    ... // code goes here
})(window);

window 肯定是没问题, 表示 BOM 浏览器对象模型中的 window 对象。但是这里为什么会有一个名为 undefined 的形参呢?起初的时候很不理解。去技术群请教了一下,才真正理解了这里的原因。

原来,Javascript 中的 undefined 并不是作为关键字(全部Javascript关键字列表)出现的。因此可以允许用户对其赋值。例如:

1
var undefined = 'myValue';

如此一来,假如 jQuery 中使用下面的写法:

1
2
3
(function( window ) {
    ... // code goes here
})(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.

- (unary)

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_newwindow

    在jQuery库中,处理弹出窗口的功能强大且灵活,使得开发者能够轻松实现各种效果。本文将深入探讨jQuery弹出窗口的使用方法,以及如何在实际项目中有效地利用它。 一、jQuery弹出窗口基础 jQuery弹出窗口并非内置...

    jquery重写window alert 信息提示

    因此,"jQuery重写window alert 信息提示"这个主题就是关于如何使用jQuery和相关库来创建更美观、功能更丰富的自定义提示框。 首先,我们需要理解jQuery的基本用法。jQuery是一个轻量级的JavaScript库,它简化了DOM...

    jquery点击弹窗2种写法

    首先,你需要在项目中引入jQuery和jQuery UI的相关库文件。在HTML页面中添加以下链接: ```html <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> ...

    弹出层的例子(含jquery.DOMWindow脚本)

    这里我们关注的是一个包含`jquery.DOMWindow`脚本的弹出层例子。`jQuery`是一个广泛使用的JavaScript库,它简化了DOM操作、事件处理以及Ajax交互等任务。`DOMWindow`则是基于jQuery的一个插件,用于创建可定制的弹出...

    JQuery插件的写法 最常用的jquery插件开发方式(教程合集).zip

    JQuery插件的写法 最常用的jquery插件开发方式(教程合集) jquery插件的例子 jQuery插件开发教程_看这一本就行了 JQuery插件写法 jq插件最常用的写法

    jQuery(document).ready(function($) { });的几种表示方法

    `jQuery(document).ready()`函数是jQuery中的一个重要概念,它确保了在执行任何JavaScript代码之前,DOM已经完全加载和解析完成。这个特性使得开发者可以在不担心元素未定义的情况下操作DOM。以下是几种常见的表示...

    jqueryzepto插件把各种延迟串联起来采用管道式写法

    "jqueryzepto插件把各种延迟串联起来采用管道式写法"这个主题,正是讨论如何通过插件的方式,巧妙地管理延迟执行的任务,使得代码更加简洁、高效。 在JavaScript中,我们常常会遇到需要按顺序执行一系列任务的情况...

    jQuery中;function($,undefined) 前面的分号的用处

    ;(function($){$.extend($.fn… ...3、因为undefined是window的属性,声明为局部变量之后,在函数中如果再有变量与undefined作比较的话,程序就可以不用搜索undefined到window,可以提高程序性能。

    ajaxFileUpload 报这错jQuery.handleError is not a function

    当你遇到“jQuery.handleError is not a function”的错误时,这意味着在使用ajaxFileUpload过程中,程序尝试调用jQuery的一个错误处理方法,但这个方法在当前版本的jQuery中并未定义。这个问题通常出现在从较旧的...

    JQuery DIV 实现window.confirm美化确认提示框

    在传统的JavaScript中,`window.confirm()`是一个内置的函数,用于弹出一个带有“确定”和“取消”按钮的对话框,供用户进行确认操作。然而,其样式单一且无法自定义,往往不符合现代网页设计的美观需求。这就是`...

    Jquery ready function Tester Source!

    Jquery ready function Tester Source!

    jquery 用法 (function($) {})(jQuery);的用法

    页面前端 jquery 用法 (function($) {})(jQuery);的用法

    jQuery链式写法

    jQuery链式写法:实现点击标签展示列表页及标签样式及内容,再次点击收起列表页还原标签样式及内容!

    jquery.DOMWindow弹出层与TAB切换实例汇总.rar

    jquery.DOMWindow弹出层与TAB切换实例汇总,jquery.DOMWindow.js是浮动弹出框的核心部件,本插件的弹出框有多种形式,比如它可以弹出不带边框的、带有淡入淡出特效的、各种颜色的背景浮动框、弹出后背景会变暗的浮动...

    jquery window

    标题 "jquery window" 指的是 jQuery 中与浏览器窗口(window 对象)相关的操作和功能。jQuery 是一个广泛使用的 JavaScript 库,它简化了 JavaScript 的 DOM 操作、事件处理、动画制作等任务。在 jQuery 中,`...

    jQueryUI MetroUI WindowUI 主题

    "jQueryUI MetroUI WindowUI"主题将这些设计理念融入到jQuery UI组件中,使得这些组件在外观上更接近Windows 8的风格。例如,对话框可能具有平坦的边框和大胆的背景色,按钮则可能拥有清晰的高亮效果,这都是Metro ...

    JQuery调用绑定click事件的3种写法

    本文将详细介绍在jQuery中绑定`click`事件的三种常见方法,以及它们的应用场景和差异。 ### 1. `click()`方法 这是最直接和最简单的绑定`click`事件的方式。当你希望为一个元素添加点击事件监听器时,可以使用`...

    jQuery中(function($){})(jQuery)详解

    在jQuery的代码中,我们经常看到一种特殊的语法结构 `(function($){})(jQuery)`,这被称为立即执行函数表达式(IIFE,Immediately Invoked Function Expression)。这个结构在jQuery插件开发中尤为常见,因为它提供...

    JQUERY-window弹出框-组件v1.0

    在这个“JQUERY-window弹出框-组件v1.0”中,我们聚焦于一个特定的功能:创建可配置的弹出窗口,具备关闭和最大化等常见操作。 首先,jQuery插件是扩展jQuery功能的代码模块,它们通常包含一系列方法和函数,用于...

Global site tag (gtag.js) - Google Analytics