在JavaScript函数中名为"undefined"的参数的用途是什么?

JaP*_*k14 17 javascript undefined

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

(function( window, undefined)  {
  ...code here
})( window );
Run Code Online (Sandbox Code Playgroud)

Pra*_*Nag 18

这是做什么,重新分配undefinedundefined封闭内部.这是一个失败的安全措施.因为其他代码可能会意外地做类似的事情

undefined = something;
console.log(undefined); // will output 'something'
Run Code Online (Sandbox Code Playgroud)

并且那在javascript有效(如果使用的JS发动机还没有执行的ECMAScript 5规范,ECMAScript中5规格undefined是非non-writable,MDN DOC),

自MDN New_in_JavaScript 1.8.5(ECMA 5)页面

对全局对象的更改

全局对象是只读的

根据ECMAScript 5规范,NaN,Infinity未定义的全局对象已经是只读的.

来自ES5 Annotated SpecGuthub

ES5 spec Section x15.1.1.3

15.1.1.3 undefined

未定义的值未定义(参见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
})();
Run Code Online (Sandbox Code Playgroud)

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

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

因此他们正在调用匿名函数之类的

( window ) // one argument only
Run Code Online (Sandbox Code Playgroud)

并接受

( 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
Run Code Online (Sandbox Code Playgroud)

这意味着在闭包内部undefined恢复undefined,因为它没有传递任何值,从而确保undefined在匿名函数内使用.

关于这个http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/的一篇非常好的详细文章

我引用上面文章链接中的一些内容来说清楚

什么是未定义的?

在JavaScript中有Undefined(type),undefined(value)undefined(variable).

未定义(类型)是内置的JavaScript类型.

undefined(value)是一个原语,是Undefined类型的唯一值.

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

没有return语句的函数或带有返回语句的函数返回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"
Run Code Online (Sandbox Code Playgroud)

undefined(variable)是一个全局属性,其初始值是undefined(value),因为它是一个全局属性,我们也可以将它作为变量访问.为了保持一致性,我总是在本文中将其称为变量.

typeof undefined; //"undefined"
var f = 2;
f = undefined; //re-assigning to undefined (variable)
typeof f; //"undefined"
Run Code Online (Sandbox Code Playgroud)

从ECMA 3开始,可以重新分配其值:

undefined = "washing machine"; //assign a string to undefined (variable)
typeof undefined //"string"
f = undefined;
typeof f; //"string"
f; //"washing machine"
Run Code Online (Sandbox Code Playgroud)

毋庸置疑,将值重新分配给未定义的变量是非常糟糕的做法,事实上ECMA 5不允许这样做.