如果argument[0].recordCount
大于零或未定义,则我希望运行代码。然而,当argument[0].recordCound
弹出未定义时,代码仍会运行。
if(arguments[0].recordCount > 0 &&
arguments[0].recordCount !== 'undefined')
{ //if more than 0 records show in bar
document.getElementById('totalRecords').innerHTML =
arguments[0].recordCount + " Records";
}
在这里,我该如何测试未定义变量?
!==
运算符而不是!=
? - stakx - no longer contributingstakx: no. typeof保证返回一个字符串,而您正在将其返回值与另一个字符串进行比较,因此无论您学习了什么Crockfordian的习惯,它始终都会很好 :)
- Tim Downtypeof
返回一个字符串。 - Peter Bailey==
或!=
时,我就会认为应该使用严格的运算符。 - stakx - no longer contributingtypeof var
与=== undefined
(后者比前者更快)。http://jsperf.com/type-of-undefined-vs-undefined - msanford