【发布时间】:2016-07-01 23:32:26
【问题描述】:
NodeJS(最新)。
我有以下代码。为什么第一个 IF 语句没有按预期工作?该控件不会进入第一个 IF 语句。
我在以下代码的第一行看到有效的 console.log 输出,并且期望第一个 IF 语句也应该执行它的代码。但事实并非如此;第二个 IF 语句有效。
console.log("-- inside create IP() qData['osType'] is set to :: " + qData['osType'])
//--
if ( qData['osType'] == 'undefined' ) {
console.log("1 -- setting qData['osType'] = Linux by default for now. This should happen automatically.")
qData['osType'] = 'Linux'
console.log("1 -- inside create IP() if-statement-qData['osType'] and qData['osType'] is set to :: "+qData['osType'])
}
if ( typeof qData['osType'] == 'undefined' ) {
console.log("2 -- setting qData['osType'] = Linux by default for now. This should happen automatically.")
qData['osType'] = 'Linux'
console.log("2 -- inside create IP() if-statement-qData['osType'] and qData['osType'] is set to :: "+qData['osType'])
}
qData['osType'] = 'Linux'
//--
【问题讨论】:
-
了解您的
qData对象是什么样子会有所帮助 -
嗯,因为
qData['osType'] != (typeof qData['usType']) -
请注意,这与 Node.js 无关,这只是标准的 JS 行为。
-
谢谢,我同意 nnnnnn 是 JS
标签: javascript if-statement undefined typeof