JavaScript 可以接受反向 switch 语句吗?

发布于 2024-12-08 12:38:24 字数 617 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绾颜 2024-12-15 12:38:24

就我个人而言,我不希望在代码库中看到反向的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 plain if/elseif block, and its exotic nature can be cause for confusion.

That's also what JSLint is complaining about:

You are doing something unorthodox. Is there a good reason for it? If
not, it might be better to stick to the basics.

禾厶谷欠 2024-12-15 12:38:24

ECMA-262 标准的第三版(受 Firefox 1.0+、Google Chrome 1.0+、MSIE 5.5+ 等支持)定义,

switch (expression) {
    case label1:
        statements1
    .
    .
    .
}

如果 (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

switch (expression) {
    case label1:
        statements1
    .
    .
    .
}

executes statements1 if (expression) matches label1.

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...

江挽川 2024-12-15 12:38:24
numberOfColumns = Math.max(4, Math.floor(menuLinksLength / 3));

这将为您提供与现有代码相同的结果。你的问题相当模糊,因为“可接受”是一个非常主观的术语。我个人会拒绝任何带有反向 switch 语句的合并请求,因为我实际上无法想到不能用更简单和/或更易于阅读的内容来替换它的情况。

numberOfColumns = Math.max(4, Math.floor(menuLinksLength / 3));

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.

情徒 2024-12-15 12:38:24

“反向开关”是否被认为是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文