如果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?
当前回答
更新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纳秒的差异并不重要。在随后的测试中,时间相当一致。
其他回答
您可以使用typeof,如下所示:
if (typeof something != "undefined") {
// ...
}
就我个人而言,我总是使用以下方法:
var x;
if( x === undefined) {
//Do something here
}
else {
//Do something else here
}
window.undefined属性在所有现代浏览器(JavaScript 1.8.5或更高版本)中都是不可写的。Mozilla文档中:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined,我看到了这一点:使用typeof()的一个原因是,如果尚未定义变量,它不会抛出错误。
我更喜欢使用
x === undefined
因为如果x之前没有声明过,它会失败并在我面前爆炸,而不是默默地通过/失败。这提醒我x未声明。我认为JavaScript中使用的所有变量都应该声明。
var x;
if (x === undefined) {
alert ("I am declared, but not defined.")
};
if (typeof y === "undefined") {
alert ("I am not even declared.")
};
/* One more thing to understand: typeof ==='undefined' also checks
for if a variable is declared, but no value is assigned. In other
words, the variable is declared, but not defined. */
// Will repeat above logic of x for typeof === 'undefined'
if (x === undefined) {
alert ("I am declared, but not defined.")
};
/* So typeof === 'undefined' works for both, but x === undefined
only works for a variable which is at least declared. */
/* Say if I try using typeof === undefined (not in quotes) for
a variable which is not even declared, we will get run a
time error. */
if (z === undefined) {
alert ("I am neither declared nor defined.")
};
// I got this error for z ReferenceError: z is not defined
// x has not been defined before
if (typeof x === 'undefined') { // Evaluates to true without errors.
// These statements execute.
}
if (x === undefined) { // Throws a ReferenceError
}
更新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纳秒的差异并不重要。在随后的测试中,时间相当一致。