开发者

Implicit cast/number check produces 'unexpected X' JSLint error

开发者 https://www.devze.com 2023-02-09 22:39 出处:网络
this is nothing serious, but just a question out of curiosity. The following script in JSLINT.com gives a strange and \'unexpected\' error. My script works, but I would still like to know if anyone ca

this is nothing serious, but just a question out of curiosity. The following script in JSLINT.com gives a strange and 'unexpected' error. My script works, but I would still like to know if anyone can explain the error.

var hashVar = 开发者_如何学PythonparseInt(location.hash.replace('#',''), 10);
if(hashVar-0 === hashVar){L();}

ERROR: Problem at line 3 character 4: Unexpected 'hashVar'.

Enjoy the weekend, Ulrik


You probably want this:

var hashVar = parseInt(location.hash.replace('#', ''), 10);
if ( !isNaN(hashVar) ) { L(); } 

This code has the same functionality as your original code.


BTW, this:

if ( !isNaN(hashVar) ) { L(); }

can be further reduced to this:

isNaN(hashVar) || L();

;-)


Explanation:

The return value of parseInt can be:

a) an integer numeric value
b) the NaN value

Therefore, if you want to test whether the return value is an integer or not, just use isNaN().

0

精彩评论

暂无评论...
验证码 换一张
取 消