如何在JSLint中禁用奇怪条件检查? [英] How to disable the Weird Condition check in JSLint?
问题描述
我有一些代码将键盘键映射到不同长度的代码块,从简单的 return false
到主要的2-3行代码片段到复杂的代码块函数:
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
.
是否存在禁用检查开关(true)
的选项(因为有其他检查)?
Is there an option to disable the check for switch(true)
(as there are for other checks)?
相关的,但不是重复:反向切换语句是否可接受JavaScript?
Related, but not a duplicate: Is a reversed switch statement acceptable JavaScript?
推荐答案
两个想法:
1)你可以让linter开心通过在文件顶部添加此注释: / * jshint eqeqeq:true * /
这就是说,我仍然得到这个'开关'应该是'if'。
警告。可能是jslint / jshint不会忽略这种构造。
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)我会恭敬地建议你抓住这个机会而不是转换linter选项清理相当奇怪的代码并重写开关
,如下所示:
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) {
//
}
这篇关于如何在JSLint中禁用奇怪条件检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!