当我的框架使用jshint来验证我的javascript
代码你好我有一个麻烦。我已经使用的switch-case没有break语句故意,但这个代码部分被捕获为错误时jshint
检查。我的代码是一样的东西下面。
switch (<no>){
case 1:
// does something
case 2:
//does something more
default:
// does something even more
}
Error from 'jshint' is like Line 203 character 41: Expected a 'break' statement before 'case'.
如何避免它有什么想法?或者是一个不好的做法,在所有使用开关的情况下在这种情况下?
复制和粘贴from the documentation:
switch语句
默认情况下,当你忽略switch语句中休息或return语句JSHint警告:
[...]
如果你真的知道你在做什么,你可以告诉JSHint您的预期情况下,块通过添加
/* falls through */
评论告吹
所以你的情况:
switch (<no>) {
case 1:
// does something
/* falls through */
case 2:
//does something more
/* falls through */
default:
// does something even more
}
没错,break
s可能完全是多余的,就像这个例子
function mapX(x){
switch (x){
case 1:
return A;
case 2:
return B;
default:
return C;
}
}
在这种情况下,如果你将不得不break
后return
,JS Standard会抛出一个警告,即Unreachable code
。
试图调解jshint和JS标准是棘手,但指出,该解决方案将是
function mapX(x){
switch (x){
case 1:
return A;
/* falls through */
case 2:
return B;
/* falls through */
default:
return C;
}
}