javascript switch() 或 if()
如果我这样做会更好:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您预见到需要添加大量新案例,您可以使用
switch
。如果您不会添加许多新案例,为了清楚起见,我可能会这样做:(
另请注意,您对“无效 ID”的检查不会执行任何操作)
不过,如果您必须添加新内容,请注意您没有添加新内容是件好事重复自己不必重复自己不必重复自己,还有性感的格式:
想象一下否则你会遇到的所有那些丑陋的else和else-if。
旁注:
如果您的规则非常复杂,但仍然是单标志上的白名单-黑名单范例,那么:
如果您想动态构建白名单,您也可以这样做。
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:
(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:
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:
You might also do this if you wanted to dynamically construct your whitelist.
这取决于你对更好的定义。你想要更好的阅读体验还是更好的性能?
我总是 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.
在某些情况下,
switch
语句比if/else
更高效/更具表现力。虽然以下if/else
语句可以轻松转换为:
另一方面,此
switch
语句会导致令人难以置信的低效
if/else
语句(switch
更高效):或者需要一个带有附加变量的
if/else
,该变量专门为此目的而声明:请注意,附加元素(如变量)会导致更多复杂性和复杂性使得程序更容易出错。
switch
语句不需要这样的变量,因为它更具表现力。The
switch
statement is more efficient/expressive thanif/else
in some cases. While the followingif/else
statementcan be easily converted to:
this
switch
statement on the other handresults in an incredible inefficient
if/else
statement (switch
is more efficient):or requires an
if/else
with additional variable, which is declared for this purpose exclusively: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.如果您在同一变量上处理一长串可能的条件,则 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.
如果您使用 if 语句,我个人更喜欢在 if 之上设置默认值,如下所示:
这样,您始终默认为安全行为,如果稍后添加更多选项,则该行为不太可能被破坏。此外,您可以一目了然地看到默认行为,而无需仔细阅读 if-then-else 逻辑。而且代码要短得多。
If you go with the if statement, I personally prefer setting default values above the if, like this:
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.
三元?
is_valid.accepted = (message !== '无效 ID') ?真:假;
Ternary?
is_valid.accepted = (message !== 'invalid id') ? true : false;