How to disable the Weird Condition check in JSLint?

这一生的挚爱 提交于 2019-12-02 17:19:46

问题


I have some code that maps keyboard keys to blocks of code of varying length, from something as simple as return false, to mainly 2-3 line snippets, to complex functions:

var k = window.event.keycode;

switch(true){
    case(k === 9):
       //
    break;

    case(k === 13):
       //
    break;

    case(k === 38 && selectedRecord > 1):
       //
    break;
}

I'd rather not create an object with a list of functions and map it that way, because they're mainly short blocks of code and there is the odd complex case.

Is there an option to disable the check for switch(true) (as there are for other checks)?

Related, but not a duplicate: Is a reversed switch statement acceptable JavaScript?


回答1:


Two thoughts:

1) You can make the linter happy by adding this comment at the top of the file: /*jshint eqeqeq: true */ That said, I still get a This 'switch' should be an 'if'. warning. It may be that jslint/jshint just won't ignore this kind of construct.

2) Instead of turning of linter options, I would respectfully suggest that you take this opportunity to clean up rather strange code and rewrite your switch as follows:

var k = window.event.keycode;

if(k === 9) {
       //
} else if(k === 13) {
       //
} else if(k === 38 && selectedRecord > 1) {
       //
}


来源:https://stackoverflow.com/questions/24233814/how-to-disable-the-weird-condition-check-in-jslint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!