JavaScript 可以接受反向 switch 语句吗?
JSLint 抱怨 (true)
是一个奇怪的条件
。如果我没有在反向 switch 语句上使用它,这是可以理解的。那么 JSLint 是错误的还是我不应该使用反向 switch 语句?
感谢您的任何帮助/启发。
switch (true) {
case (menuLinksLength < 4):
numberOfColumns = 1;
break;
case (menuLinksLength > 3 && menuLinksLength < 7):
numberOfColumns = 2;
break;
case (menuLinksLength > 6 && menuLinksLength < 10):
numberOfColumns = 3;
break;
case (menuLinksLength > 9):
numberOfColumns = 4;
break;
default:
numberOfColumns = 0;
}
JSLint is complaining that (true)
is a weird condition
. Which is understandable if I wasn't using it on a reversed switch statement. So is JSLint wrong or should I not be using reversed switch statements?
Thanks for any help/enlightenment.
switch (true) {
case (menuLinksLength < 4):
numberOfColumns = 1;
break;
case (menuLinksLength > 3 && menuLinksLength < 7):
numberOfColumns = 2;
break;
case (menuLinksLength > 6 && menuLinksLength < 10):
numberOfColumns = 3;
break;
case (menuLinksLength > 9):
numberOfColumns = 4;
break;
default:
numberOfColumns = 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我个人而言,我不希望在代码库中看到反向的
switch
。与普通的if/elseif
块相比,它不会给您带来任何好处,而且其奇异的性质可能会导致混乱。这也是 JSLint 抱怨的地方:
Personally I wouldn't like seeing reversed
switch
in a code base. It doesn't buy you anything when compared to a plainif/elseif
block, and its exotic nature can be cause for confusion.That's also what JSLint is complaining about:
ECMA-262 标准的第三版(受 Firefox 1.0+、Google Chrome 1.0+、MSIE 5.5+ 等支持)定义,
如果
(expression)
匹配,则执行statements1
标签1。这意味着您的
switch
语句完全没问题。我在 Firefox、Chrome 和 IE 上尝试过。没有人抱怨...
编辑:
现在是猜测部分:
JSLint 是一个代码分析工具。当它看到
switch (true)
时,它会假设您不知道自己在做什么。 奇怪并不意味着一定错误...The third edition of the ECMA-262 standard (supported by Firefox 1.0+, Google Chrome 1.0+, MSIE 5.5+ and others) defines that
executes
statements1
if(expression)
matcheslabel1
.That means that your
switch
statement is perfectly fine.I tried it out on Firefox, Chrome and IE. None complains...
Edit:
Now the guessing part:
JSLint is a code anaylisis tool. When it sees
switch (true)
, it assumes that you don't know what you're doing. Weird doesn't mean necessarily wrong...这将为您提供与现有代码相同的结果。你的问题相当模糊,因为“可接受”是一个非常主观的术语。我个人会拒绝任何带有反向 switch 语句的合并请求,因为我实际上无法想到不能用更简单和/或更易于阅读的内容来替换它的情况。
This will give you identical results to your existing code. Your question is fairly ambiguous, as "acceptable" is a very subjective term. I would personally reject any merge request with a reversed switch statement, because I actually can't think of a situation where that couldn't be replaced with something simpler and/or easier to read.
“反向开关”是否被认为是 JavaScript 中的良好实践取决于具体的用例。
一般来说,反向开关可以通过明确定义与特定模式不匹配的情况的预期行为,使代码更具可读性和可维护性。当您想要捕获 switch 语句中未显式定义的所有边缘情况或异常时,这非常有用。
Whether or not a "reverse switch" is considered good practice in javascript depends on the specific use case.
In general, a reverse switch can make the code more readable and maintainable by clearly defining the intended behaviour for cases that don't match a specific pattern. This can be useful in situations where you want to catch all edge cases or exceptions that are not explicitly defined in the switch statement.