NodeJS / JavaScript if语句不起作用

AKS

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'
  //--
mscdex

如果要检查不确定性,可以执行以下操作之一:

  • typeof foo === 'undefined'
  • foo === undefined
  • foo === void 0

实际上,其他所有内容(严格地)都不会检查未定义的值(包括直接将值与string进行比较'undefined')。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章