反向切换语句是否可以接受JavaScript

反向切换语句是否可以接受JavaScript

本文介绍了反向切换语句是否可以接受JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSLint抱怨(true)奇怪的条件。如果我没有在反向切换声明中使用它,这是可以理解的。那么JSLint是错误的还是我不应该使用反向开关语句?

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?

感谢您的帮助/启发。

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;
}


推荐答案

第三版ECMA -262标准(由Firefox 1.0 +支持,谷歌Chrome 1.0+,MSIE 5.5+等)定义

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

执行 statements1 if (表达式)匹配 label1

这意味着您的开关语句完全正常。

That means that your switch statement is perfectly fine.

我在Firefox,Chrome和IE上试用过它。无抱怨......

I tried it out on Firefox, Chrome and IE. None complains...

编辑:

现在猜测部分:

JSLint是一个代码anaylisis工具。当它看到 switch(true)时,它假定你不知道你在做什么。 奇怪并不一定意味着错误 ......

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

这篇关于反向切换语句是否可以接受JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:44