jQuery核心风格指南提供了两种不同的方法来检查变量是否被定义。
全局变量:typeof variable === "undefined" 局部变量:variable === undefined 属性:对象。Prop === undefined
为什么jQuery对全局变量使用一种方法,而对局部变量和属性使用另一种方法?
jQuery核心风格指南提供了两种不同的方法来检查变量是否被定义。
全局变量:typeof variable === "undefined" 局部变量:variable === undefined 属性:对象。Prop === undefined
为什么jQuery对全局变量使用一种方法,而对局部变量和属性使用另一种方法?
当前回答
我将坚持在任何地方使用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检查。
其他回答
谁对变量=== undefined的性能增益感兴趣,可以在这里看看,但这似乎只是一个chrome优化。
http://jsperf.com/type-of-undefined-vs-undefined/30 http://jsperf.com/type-of-undefined-vs-undefined
我将坚持在任何地方使用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检查。
简介:
当处于全局作用域时,如果变量没有声明或值为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);
在节点v6.9.1上,Typeof a === 'undefined'比a === 'undefined'快大约2倍。
对于未声明的变量,typeof foo将返回字符串字面量“undefined”,而标识检查foo === undefined将触发错误“foo is not defined”。
对于局部变量(您知道在某处声明了),不会发生这样的错误,因此需要进行标识符检查。