2012-06-23 72 views
16

在查看jQuery的未压缩的源代码,我偶然发现了一些我不太了解。当创建他们的匿名函数时,他们将undefined作为第二个参数。这是干什么的,为什么他们使用未定义的?是否有必要将undefined作为参数放入匿名函数中?以下是我正在谈论的一个例子。JavaScript函数中名为“undefined”的参数的用途是什么?

(function(window, undefined) { 
    ...code here 
})(window); 
+1

相关:http://stackoverflow.com/q/5020479/ – CMS

+0

相关:[为什么window.undefined在旧版浏览器中可变?](https://stackoverflow.com/q/29742135/1048572) – Bergi

回答

17

这是什么做的,是重新分配到undefinedundefined封内。那是失败的保险。因为其他代码可能会意外地做这样的事情

undefined = something; 
console.log(undefined); // will output 'something' 

而且在JavaScript这就是有效的(如果使用的JS引擎一直没有实现的ECMAScript 5规范,在ECMAScript中5规格undefinednon-writableMDN DOC),

从MDN New_in_JavaScript 1.8.5(ECMA 5)第

报价

更改全局对象

全球对象进行只读

NaN的无限,并不确定全球 对象已作出只读,根据ECMAScript 5规范。

而且从ES5 Annotated Spec在Guthub

x15.1.1.3ES5 spec Section

15.1.1.3未定义

的未定义的值是未定义的(见8.1)。

该属性具有属性{[[Writable]]:false,[[Enumerable]]:false,[[Configurable]]:false}。

即使global undefined不可写你可以有一个名为undefined局部变量,可以搞砸你的代码(主要是与undefined比较)。 但这是你的责任。你可以有代码像

(function(){ 
    console.log('Second Case: '); 
    var undefined = 'Something'; 
    console.log(undefined); // Will log `something` 
    var a ; // a is undefined 
    console.log(a === undefined); // false, as undefined is changed 
    // you might expect a === undefined will return true, but as 
    // `undefined` is changed it will return false. 
    console.log(a); // undefined 
})(); 

演示:http://jsfiddle.net/joycse06/V4DKN/

但是,如果undefined是可写的,然后上面的分配可能会妨碍许多comparison该行代码为undefinedundefined上做出了不undefined了。它现在有一些价值。

所以他们呼吁像

(window) // one argument only 

匿名函数以及接收

(window, undefined) // only window is passed when calling the function 
      // Second argument is not passed means it's undefined 
      // so undefined is restored to undefined inside that function 
      // and no global accidental assignments can hamper jQuery's 
      // code using 'undefined' now 

这是封闭undefined内侧,是指恢复到undefined,因为它并没有被通过从而确保任何价值在那个匿名函数里面使用undefined

这个http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/

一个非常好的详细的文章中,我引用从上面的文章链接,把事情说清楚

什么是不确定的某些行?

在JavaScript中有未定义(类型)未定义(值)未定义(变量)。

未定义(类型)是一种内置的JavaScript类型。

未定义(值)是一个原始的和是未定义类型的唯一值。

任何尚未分配值的属性假定未定义的值。 (ECMA 4.3.9 和4.3.10)。

没有返回语句的函数或具有空返回语句的函数返回undefined。未定义函数参数的值未定义。

var a; 
typeof a; //"undefined" 

window.b; 

typeof window.b; //"undefined" 



var c = (function() {})(); 

typeof c; //"undefined" 



var d = (function(e) {return e})(); 

typeof d; //"undefined" 

未定义(变量)是一个全球性的属性,它的初始值是未定义(值),因为它的全球性质,我们也可以访问它作为一个变量。为了一致性,我总是在本文中将其称为变量。

typeof undefined; //"undefined" 
var f = 2; 
f = undefined; //re-assigning to undefined (variable) 
typeof f; //"undefined" 

由于ECMA 3中,其值可以被重新分配:

undefined = "washing machine"; //assign a string to undefined (variable) 
typeof undefined //"string" 
f = undefined; 
typeof f; //"string" 
f; //"washing machine" 

不用说,重新分配给不确定的变量值是非常不好的做法,事实上,它不是由ECMA允许5.

+1

console.log(undefined)..或不是? 是的,就像那样:) – Luceos

+1

@Luceos,谢谢先生指出错字。 :) –

+0

所以,例如,如果我有一个匿名函数外“var undefined = 1”,并且我有一个匿名函数,看起来像这样:(function(window,undefined){})(window); ...是你说undefined = 1现在有一个未定义的值? – JaPerk14

3

未定义是一个类型,但也是一个全局变量。

你可以有覆盖的做undefined = whatever值undefined的模块。

undefined实际上是一个功能的包装整个代码一个未定义的参数:

(function(window, undefined) { 
    // undefined is the undefined parameter 
}(window)); 

它是安全的,因为undefined参数是在局部范围,且任何人,除非在此功能的代码可以分配到它。

这是没有必要使用undefined作为参数时,定义了一个匿名函数。

如果你看到上面的函数,你无线本地环路注意到它预计双参数一个供应。

为什么undefined需要恢复?

因为,为了可以确保undefined的确是在大括号之间的范围undefined,即使有人写类似的东西在全球范围内undefined = "defined";,因为undefined实际上可以被重新定义。

所以,如果你有类似

var undefined = 1; 

(function(window, undefined) { 
    console.log(undefined); // output will be undefined not 1 
}(window)); 
+0

你不会解释为什么undefined需要恢复,其他答案也可以。 – Luceos

2

jQuery是确保undefined真的是在其范围内通过不将参数传递给该参数未定义。

拿这个代码,例如当undefined没有确切定义:

var undefined = 1; 
alert(undefined); // 1 
相关问题