读书人

关于 jQuery中 function( window, und

发布时间: 2012-10-08 19:54:56 作者: rapoo

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

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

;

的代码进行赋值,那么,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

读书人网 >Web前端

热点推荐