名为“的参数的用途是什么?”;“未定义”;在JavaScript函数中?

名为“的参数的用途是什么?”;“未定义”;在JavaScript函数中?,javascript,undefined,Javascript,Undefined,在查看jQuery的未压缩源代码时,我偶然发现了一些我不太理解的东西。当创建匿名函数时,它们将undefined作为第二个参数。这是做什么的,为什么他们使用未定义的?是否有必要将undefined作为参数放入匿名函数中?下面是我正在谈论的一个例子 (function( window, undefined) { ...code here })( window ); Undefined是一个类型,但也是一个全局变量 您可以使用一个模块,通过执行undefined=which来覆盖undefi

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

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

Undefined是一个类型,但也是一个全局变量

您可以使用一个模块,通过执行
undefined=which
来覆盖undefined的值

中的
未定义
实际上是包装整个代码的函数的未定义参数:

(function(window, undefined) {
    // undefined is the undefined parameter
}(window)); 
这是安全的,因为
undefined
参数在本地范围内,除了此函数中的代码之外,没有人可以分配给它

定义匿名函数时,不必使用
未定义的
作为参数

如果您看到上面的函数,您将注意到它需要两个参数,但提供了一个参数

为什么
未定义
需要恢复?

因为,为了确保
undefined
确实在大括号之间的范围内
undefined
,即使有人写了类似
undefined=“defined”的东西在全局范围内,因为
未定义
实际上可以重新定义

如果您有类似的

var undefined = 1;

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

jQuery通过不向该参数传递参数来确保
undefined
在其作用域内确实是未定义的

未定义
实际上不是未定义时,以该代码为例:

var undefined = 1;
alert(undefined); // 1

这样做的目的是将
undefined
重新分配给该闭包中的
undefined
。这是故障保险。因为其他代码可能会意外地执行以下操作

undefined = something;
console.log(undefined); // will output 'something'
这在javascript中是有效的(如果使用的JS引擎没有实现ECMAScript 5规范,那么在ECMAScript 5规范中,
未定义的
是不可写的
,)

引用MDN(ECMA 5)第页

对全局对象的更改

使全局对象成为只读对象

NaN无限未定义的全局 根据ECMAScript 5规范,对象已成为只读对象

以及来自Guthub中的ES5注释规范

ES5规范部分

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
})();
演示:

但是,如果
未定义的
是可写的,则上述赋值可能会妨碍在该行代码之后与
未定义的
进行的许多
比较,因为
未定义的
不再是
未定义的
。现在它有了一些价值

所以当他们调用匿名函数时

( 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

这是一篇非常详细的文章

我引用了上面文章链接中的几行来说明问题

什么是未定义的?

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

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

未定义(值)是一个原语,是未定义类型的唯一值

任何未指定值的属性都假定为未定义的值。(ECMA 4.3.9) 和4.3.10)

没有return语句的函数,或return语句为空的函数返回未定义的值。未应用函数参数的值未定义

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不允许这样做。

console.log(未定义)。。还是不?是的,像这样:)例如,如果我在一个匿名函数外有“var undefined=1”,我有一个匿名函数,看起来是这样的:(function(window,undefined){}(window);。。。你是说undefined=1现在有一个undefined的值吗?@JaPerk14,检查更新的答案,我引用了文章中的一些行,为了深入理解,我建议你阅读整篇文章。@JaPerk14,如果你有一个
undefined=1
;在全局范围中,如果不使用
(函数(窗口,未定义){})(窗口)
undefined在该函数中也会有一个值
1
(当您更改全局
undefined
变量时)。这是不可取的,因为
未定义
应该是
未定义
。因此,为了确保在函数内部使用
模式
。您没有解释为什么需要恢复未定义,其他答案会。相关:相关: