Implicit cast/number check produces 'unexpected X' JSLint error
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()
.
精彩评论