jQuery核心风格指南提供了两种不同的方法来检查变量是否被定义。

全局变量:typeof variable === "undefined" 局部变量:variable === undefined 属性:对象。Prop === undefined

为什么jQuery对全局变量使用一种方法,而对局部变量和属性使用另一种方法?


当前回答

使用typef -variant的另一个原因是:undefined可以被重新定义。

undefined = "foo";
var variable = "foo";
if (variable === undefined)
  console.log("eh, what?!");

typeof变量的结果不能。

更新:注意,在ES5中不是这样的,全局未定义是一个不可配置,不可写的属性:

15.1.1全局对象的值属性 […] 15.1.1.3未定义 undefined的值是undefined(参见8.1)。该属性具有以下属性 {[[可写]]:false,[[可列举]]:false,[[可配置]]:false}。

但它仍然可以被一个局部变量遮蔽:

(function() {
  var undefined = "foo";
  var variable = "foo";
  if (variable === undefined)
    console.log("eh, what?!");  
})()

或参数:

(function(undefined) {
  var variable = "foo";
  if (variable === undefined)
    console.log("eh, what?!");  
})("foo")

其他回答

简介:

当处于全局作用域时,如果变量没有声明或值为undefined,我们实际上希望返回true:

var globalVar1; //该变量声明了,但没有定义,因此值为undefined console.log(globalVar1 === undefined); //该变量未声明,因此将抛出referenceError console.log(globalVar2 === undefined);

因为在全局作用域中,我们不能100%确定是否声明了一个变量,这可能会给我们一个referenceError。当我们在未知变量上使用typeof操作符时,如果变量没有声明,就不会出现这个问题:

var globalVar1; log控制台。 log控制台。

这是因为typeof操作符在变量未声明或当前值为undefined时返回字符串未定义,而这正是我们想要的。


对于局部变量,我们就没有这个问题,因为我们事先知道这个变量是存在的。如果变量存在,我们可以简单地在相应的函数中查找。 对于对象属性,我们就没有这个问题,因为当我们试图查找一个不存在的对象属性时,我们也会得到undefined的值

var obj = {}; consoles.log(obj.myProp === undefined);

谁对变量=== undefined的性能增益感兴趣,可以在这里看看,但这似乎只是一个chrome优化。

http://jsperf.com/type-of-undefined-vs-undefined/30 http://jsperf.com/type-of-undefined-vs-undefined

使用typef -variant的另一个原因是:undefined可以被重新定义。

undefined = "foo";
var variable = "foo";
if (variable === undefined)
  console.log("eh, what?!");

typeof变量的结果不能。

更新:注意,在ES5中不是这样的,全局未定义是一个不可配置,不可写的属性:

15.1.1全局对象的值属性 […] 15.1.1.3未定义 undefined的值是undefined(参见8.1)。该属性具有以下属性 {[[可写]]:false,[[可列举]]:false,[[可配置]]:false}。

但它仍然可以被一个局部变量遮蔽:

(function() {
  var undefined = "foo";
  var variable = "foo";
  if (variable === undefined)
    console.log("eh, what?!");  
})()

或参数:

(function(undefined) {
  var variable = "foo";
  if (variable === undefined)
    console.log("eh, what?!");  
})("foo")

在节点v6.9.1上,Typeof a === 'undefined'比a === 'undefined'快大约2倍。

我将坚持在任何地方使用typeof foo === "undefined"。这永远不会出错。

I imagine the reason why jQuery recommends the two different methods is that they define their own undefined variable within the function that jQuery code lives in, so within that function undefined is safe from tampering from outside. I would also imagine that someone somewhere has benchmarked the two different approaches and discovered that foo === undefined is faster and therefore decided it's the way to go. [UPDATE: as noted in the comments, the comparison with undefined is also slightly shorter, which could be a consideration.] However, the gain in practical situations will be utterly insignificant: this check will never, ever be any kind of bottleneck, and what you lose is significant: evaluating a property of a host object for comparison can throw an error whereas a typeof check never will.

以IE中解析XML为例:

var x = new ActiveXObject("Microsoft.XMLDOM");

检查是否有loadXML方法:

typeof x.loadXML === "undefined"; // Returns false

另一方面:

x.loadXML === undefined; // Throws an error

更新

typeof检查的另一个优点是,我忘了提到它也适用于未声明的变量,而foo === undefined检查不能,实际上会抛出一个ReferenceError。谢谢@LinusKleen的提醒。例如:

typeof someUndeclaredVariable; // "undefined"
someUndeclaredVariable === undefined; // throws a ReferenceError

底线:总是使用typeof检查。