javascript switch() 或 if()

发布于 2024-11-01 21:09:39 字数 401 浏览 0 评论 0原文

如果我这样做会更好:

if(message == 'redirect')
{
    is_valid.accepted = true;
}
else if(message == 'invalid id')
{
    is_valid.accepted = false;
}
else
{
    is_valid.accepted = false;
}

或者我这样做

switch (message)
{
case 'invalid id':
default:
    is_valid.accepted = false;
    break;
case 'redirect':
    is_valid.accepted = true;
    break;
}

which would be better if i do this:

if(message == 'redirect')
{
    is_valid.accepted = true;
}
else if(message == 'invalid id')
{
    is_valid.accepted = false;
}
else
{
    is_valid.accepted = false;
}

or i do it this way

switch (message)
{
case 'invalid id':
default:
    is_valid.accepted = false;
    break;
case 'redirect':
    is_valid.accepted = true;
    break;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

别低头,皇冠会掉 2024-11-08 21:09:39

如果您预见到需要添加大量新案例,您可以使用 switch

如果您不会添加许多新案例,为了清楚起见,我可能会这样做:(

is_valid.accepted = message=='redirect';

另请注意,您对“无效 ID”的检查不会执行任何操作)

不过,如果您必须添加新内容,请注意您没有添加新内容是件好事重复自己不必重复自己不必重复自己,还有性感的格式:

switch (message)
{
    case 'invalid id':
    case 'penguin invasion':
    case 'the internet is down':
    case 'error not enough caffeine':
        is_valid.accepted = false;
        break;

    case 'redirect':
    case 'upvote me':
    case 'vip':
    case 'flamewar':
        is_valid.accepted = true;
        break;

    default:
        is_valid.accepted = false;
        // perhaps log or something
}

想象一下否则你会遇到的所有那些丑陋的else和else-if。


旁注:
如果您的规则非常复杂,但仍然是单标志上的白名单-黑名单范例,那么:

var blacklist = ['invalid id', 'penguin invasion', 'the internet is down' 'error not enough caffeine'];
var whitelist = ['redirect', 'upvote me', 'vip', 'flamewar'];

is_valid.accepted = whitelist.indexOf(message)!=-1;

如果您想动态构建白名单,您也可以这样做。

You might use switch if you foresaw needing to add lots of new cases.

If you won't be adding many new cases, I might do, for clarity:

is_valid.accepted = message=='redirect';

(also note that your check for 'invalid id' does nothing)

Nevertheless if you had to add new things, notice how it's good you don't have to repeat yourself don't have to repeat yourself don't have to repeat yourself, also the sexy formatting:

switch (message)
{
    case 'invalid id':
    case 'penguin invasion':
    case 'the internet is down':
    case 'error not enough caffeine':
        is_valid.accepted = false;
        break;

    case 'redirect':
    case 'upvote me':
    case 'vip':
    case 'flamewar':
        is_valid.accepted = true;
        break;

    default:
        is_valid.accepted = false;
        // perhaps log or something
}

Imagine all those ugly else and else-ifs you'd have otherwise.


sidenote:
If you had really complicated rules, but still a whitelist-blacklist-on-a-single-flag paradigm, then:

var blacklist = ['invalid id', 'penguin invasion', 'the internet is down' 'error not enough caffeine'];
var whitelist = ['redirect', 'upvote me', 'vip', 'flamewar'];

is_valid.accepted = whitelist.indexOf(message)!=-1;

You might also do this if you wanted to dynamically construct your whitelist.

比忠 2024-11-08 21:09:39

这取决于你对更好的定义。你想要更好的阅读体验还是更好的性能?

我总是 jsPerf 的东西。如果可读性使我的代码更快/更正确,我并不太关心它。

这是一堆不同 switch 与 if/else if/if ==/if === 语句的 jsPerf。

http://jsperf.com/switch-if-else/16

这是修订版 16测试的。因此,如果您在 10 周后查看此测试,请确保滚动到底部并获取最新的测试。

It depends on your definition of better. Do you want it to be a better reading experience or better performance?

I always jsPerf things. I don't really care much about readability if it makes my code faster/proper.

Here is a jsPerf of a bunch of different switch vs. if/else if/if ==/if === statements.

http://jsperf.com/switch-if-else/16

This is revision 16 of the test. So if you are looking at this 10 weeks from now make sure you scroll to the bottom and grab the most recent test.

深海夜未眠 2024-11-08 21:09:39

在某些情况下,switch 语句比 if/else 更高效/更具表现力。虽然以下 if/else 语句

let x = 123;

if (x) {/*...*/} // implicit type casting (to boolean)
else {/*...*/}

可以轻松转换为:

switch (!!x) { // explicit type casting (to boolean)
  case true: /*...*/ break;
  default: /*...*/
}

另一方面,此 switch 语句

function algo(x) {/*...performing a complex algorithm...*/}

switch (algo(123)) { // executed once
  case "result 1": /*...*/ break;
  case "result 2": /*...*/ break;
  case "result 3": /*...*/ break;
  default: /*...*/
}

会导致令人难以置信的低效 if/else 语句(switch 更高效):

if (algo(123) === "result 1") {/*...*/}
else if (algo(123) === "result 2") {/*...*/}
else if (algo(123) === "result 3") {/*...*/}
else {/*...*/}

或者需要一个带有附加变量的 if/else,该变量专门为此目的而声明:

let y = algo(x); // additional variable

if (y === "result 1") {/*...*/}
else if (y === "result 2") {/*...*/}
else if (y === "result 3") {/*...*/}
else {/*...*/}

请注意,附加元素(如变量)会导致更多复杂性和复杂性使得程序更容易出错。 switch 语句不需要这样的变量,因为它更具表现力。

The switch statement is more efficient/expressive than if/else in some cases. While the following if/else statement

let x = 123;

if (x) {/*...*/} // implicit type casting (to boolean)
else {/*...*/}

can be easily converted to:

switch (!!x) { // explicit type casting (to boolean)
  case true: /*...*/ break;
  default: /*...*/
}

this switch statement on the other hand

function algo(x) {/*...performing a complex algorithm...*/}

switch (algo(123)) { // executed once
  case "result 1": /*...*/ break;
  case "result 2": /*...*/ break;
  case "result 3": /*...*/ break;
  default: /*...*/
}

results in an incredible inefficient if/else statement (switch is more efficient):

if (algo(123) === "result 1") {/*...*/}
else if (algo(123) === "result 2") {/*...*/}
else if (algo(123) === "result 3") {/*...*/}
else {/*...*/}

or requires an if/else with additional variable, which is declared for this purpose exclusively:

let y = algo(x); // additional variable

if (y === "result 1") {/*...*/}
else if (y === "result 2") {/*...*/}
else if (y === "result 3") {/*...*/}
else {/*...*/}

Please note that additional elements (like variables) cause more complexity and complexity makes programs more error prone. The switch statement doesn't need such a variable, because it is more expressive.

时光沙漏 2024-11-08 21:09:39

如果您在同一变量上处理一长串可能的条件,则 Switch 会更好。在这种情况下,我认为没有太多理由使用 switch() ,除非您更喜欢这种语法。

Switch is better if you're working with a long list of possible conditions on the same variable. In this case, I don't think there's much reason to use switch() unless you prefer the syntax.

原来分手还会想你 2024-11-08 21:09:39

如果您使用 if 语句,我个人更喜欢在 if 之上设置默认值,如下所示:

is_valid.accepted = false;
if(message == 'redirect')
{
    is_valid.accepted = true;
}

这样,您始终默认为安全行为,如果稍后添加更多选项,则该行为不太可能被破坏。此外,您可以一目了然地看到默认行为,而无需仔细阅读 if-then-else 逻辑。而且代码要短得多。

If you go with the if statement, I personally prefer setting default values above the if, like this:

is_valid.accepted = false;
if(message == 'redirect')
{
    is_valid.accepted = true;
}

That way, you always default to a safe behavior that is less likely to break if you add more options later on. Also, you see the default behavior at a glance without having to read through the if-then-else logic. And it's much shorter code.

止于盛夏 2024-11-08 21:09:39

三元?
is_valid.accepted = (message !== '无效 ID') ?真:假;

Ternary?
is_valid.accepted = (message !== 'invalid id') ? true : false;

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