这不是什么严重的问题,而是一个出于好奇的问题。 JSLINT.com中的以下脚本给出了一个奇怪的“意外”错误。我的脚本有效,但我仍然想知道是否有人可以解释错误。
var hashVar = parseInt(location.hash.replace('#',''), 10);
if(hashVar-0 === hashVar){L();}
错误:第3行第4个问题:意外的'hashVar'。
享受周末,Ulrik
答案 0 :(得分:4)
你可能想要这个:
var hashVar = parseInt(location.hash.replace('#', ''), 10);
if ( !isNaN(hashVar) ) { L(); }
此代码与原始代码具有相同的功能。
if ( !isNaN(hashVar) ) { L(); }
可以进一步简化为:
isNaN(hashVar) || L();
- )
<强>解释强>
parseInt
的返回值可以是:
a)整数数值
b)NaN
值
因此,如果要测试返回值是否为整数,只需使用isNaN()
。