如果JavaScript中未定义变量,最合适的测试方法是什么?
我见过几种可能的方法:
if (window.myVariable)
Or
if (typeof(myVariable) != "undefined")
Or
if (myVariable) // This throws an error if undefined. Should this be in Try/Catch?
如果JavaScript中未定义变量,最合适的测试方法是什么?
我见过几种可能的方法:
if (window.myVariable)
Or
if (typeof(myVariable) != "undefined")
Or
if (myVariable) // This throws an error if undefined. Should this be in Try/Catch?
当前回答
我使用它作为函数参数,并在函数执行时排除它,这样我就得到了未定义的“真实”。尽管它确实需要您将代码放在函数中。我在阅读jQuery源代码时发现了这一点。
undefined = 2;
(function (undefined) {
console.log(undefined); // prints out undefined
// and for comparison:
if (undeclaredvar === undefined) console.log("it works!")
})()
当然,你可以使用typeof。但无论如何,我的所有代码通常都在一个包含函数中,所以使用这个方法可能会在这里和那里节省一些字节。
其他回答
与@Thomas Eding的回答相反:
如果我忘记在代码中声明myVar,那么我将得到myVar未定义。
让我们举一个真实的例子:
我有一个变量名,但我不确定它是否在某处声明。
那么@Anurak的回答会有所帮助:
var myVariableToCheck = 'myVar';
if (window[myVariableToCheck] === undefined)
console.log("Not declared or declared, but undefined.");
// Or you can check it directly
if (window['myVar'] === undefined)
console.log("Not declared or declared, but undefined.");
如果您有兴趣了解变量是否已声明,而不管其值如何,那么使用in运算符是最安全的方法。考虑以下示例:
// global scope
var theFu; // theFu has been declared, but its value is undefined
typeof theFu; // "undefined"
但在某些情况下,这可能不是预期的结果,因为变量或属性已声明,但尚未初始化。使用in运算符进行更稳健的检查。
"theFu" in window; // true
"theFoo" in window; // false
如果您想知道变量是否尚未声明或值未定义,请使用typeof运算符,它保证返回字符串:
if (typeof myVar !== 'undefined')
与undefined的直接比较很麻烦,因为undefineed可以被覆盖。
window.undefined = "foo";
"foo" == undefined // true
正如@CMS所指出的,这已经在ECMAScript第5版中修补,undefined是不可写的。
if(window.myVar)也将包含这些虚假值,因此它不是很健壮:
false 0 "" NaN null undefined
感谢@CMS指出您的第三种情况-if(myVariable)在两种情况下也会抛出错误。第一种情况是尚未定义引发ReferenceError的变量。
// abc was never declared.
if (abc) {
// ReferenceError: abc is not defined
}
另一种情况是定义了变量,但有一个getter函数,在调用时抛出错误。例如
// or it's a property that can throw an error
Object.defineProperty(window, "myVariable", {
get: function() { throw new Error("W00t?"); },
set: undefined
});
if (myVariable) {
// Error: W00t?
}
我知道检查undefined最可靠的方法是使用void 0。
这与较新和较旧的浏览器都兼容,并且不能像window那样被覆盖。在某些情况下,未定义的浏览器可以被覆盖。
if( myVar === void 0){
//yup it's undefined
}
更新2018-07-25
自从这篇文章发表以来,已经将近五年了,JavaScript已经取得了长足的进步。在重复原始帖子中的测试时,我发现以下测试方法之间没有一致的差异:
abc==未定义abc==无效0typeof abc==“未定义”abc类型==“未定义”
即使当我修改测试以防止Chrome优化它们时,差异也微不足道。因此,为了清楚起见,我现在建议abc===undefined。
相关内容来自chrome://version:
谷歌Chrome:67.0.3396.99(官方版本)(64位)(队列:稳定)修订:a337fbf3c2ab8ebc6b64b0bfdce73a20e2e2252b参考/分支头/3396@{#790}操作系统:WindowsJavaScript:V8 6.7.288.46用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/67.03396.99 Safari/537.36
原帖子2013-11-01
在Google Chrome中,以下测试比一种测试速度稍快:
if (abc === void 0) {
// Undefined
}
差异可以忽略不计。然而,这段代码更加简洁,对于了解void 0含义的人来说一目了然。然而,请注意,abc仍然必须声明。
typeof和void都明显快于直接与undefined进行比较。我在Chrome开发者控制台中使用了以下测试格式:
var abc;
start = +new Date();
for (var i = 0; i < 10000000; i++) {
if (TEST) {
void 1;
}
}
end = +new Date();
end - start;
结果如下:
Test: | abc === undefined abc === void 0 typeof abc == 'undefined'
------+---------------------------------------------------------------------
x10M | 13678 ms 9854 ms 9888 ms
x1 | 1367.8 ns 985.4 ns 988.8 ns
请注意,第一行以毫秒为单位,而第二行以纳秒为单位。3.4纳秒的差异并不重要。在随后的测试中,时间相当一致。
由于其他答案对我都没有帮助,我建议这样做。它在Internet Explorer 8中对我有效:
if (typeof variable_name.value === 'undefined') {
// variable_name is undefined
}