我可以使用带有两个变量的 case/switch 语句吗?
我是 JavaScript 的新手,据我了解,使用一个 SWITCH/CASE 语句比一大堆 IF 语句更快。
但是,我想使用带有两个变量的 SWITCH/CASE 语句。
我的网络应用程序有两个滑块,每个滑块都有五个状态。我希望行为基于这两个变量的状态。显然,这是一大堆 IF/THEN 语句。
我想到的一种方法是将两个变量连接成一个,然后我可以对其进行切换/区分。
有没有更好的方法使用两个变量来完成 SWITCH/CASE ?
谢谢 !
I am a newbie when it comes to JavaScript and it was my understanding that using one SWITCH/CASE statements is faster than a whole bunch of IF statements.
However, I want to use a SWITCH/CASE statement with two variables.
My web app has two sliders, each of which have five states. I want the behavior to be based on the states of these two variables. Obviously that is a whole heck of a lot of IF/THEN statements.
One way I thought about doing it was concatenating the two variables into one and then I could SWITCH/CASE that.
Is there a better way of accomplishing a SWITCH/CASE using two variables ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
是的,你也可以这样做:
这将始终执行开关,几乎就像 if/else 但看起来更干净。只需继续检查 case 表达式中的变量即可。
Yes you can also do:
This will always execute the switch, pretty much just like if/else but looks cleaner. Just continue checking your variables in the case expressions.
按位运算符怎么样?您处理的是“枚举”,而不是字符串,它看起来更“优雅”。
然而,我同意其他人的观点,switch-case 逻辑中的 25 个案例不太漂亮,而 if-else 在某些情况下可能“看起来”更好。反正。
How about a bitwise operator? Instead of strings, you're dealing with "enums", which looks more "elegant."
I however agree with others, 25 cases in a switch-case logic is not too pretty, and if-else might, in some cases, "look" better. Anyway.
如果每一种都有 5 种可能性,那么您将得到 25 种情况。
If you have 5 possibilities for each one you will get 25 cases.
首先,JavaScript 的
switch
并不比if/else(有时慢得多)
。
其次,将
switch
与多个变量一起使用的唯一方法是将它们组合成一个原始(字符串、数字等)值:但是,就我个人而言,我宁愿看到一组
if< /code>/
else
语句。编辑:当所有值都是整数时,在 Chrome 中 switch 的性能似乎优于 if/else。看看评论。
First, JavaScript's
switch
is no faster thanif/else
(and sometimes much slower).Second, the only way to use
switch
with multiple variables is to combine them into one primitive (string, number, etc) value:But, personally, I would rather see a set of
if
/else
statements.Edit: When all the values are integers, it appears that switch can out-perform if/else in Chrome. See the comments.
我不相信 switch/case 比一系列 if/elseif 更快。它们做同样的事情,但是 if/elseif 可以检查多个变量。您不能对多个值使用 switch/case。
I don't believe a switch/case is any faster than a series of if/elseif's. They do the same thing, but if/elseif's you can check multiple variables. You cannot use a switch/case on more than one value.
如果每个组合的动作是静态的,您可以构建一个二维数组:
上例中的数字可以是任何东西,例如字符串、数组等。现在获取值是单行的(假设滑块有一个值) [0,5) 的范围:
If the action of each combination is static, you could build a two-dimensional array:
The numbers in above example can be anything, such as string, array, etc. Fetching the value is now a one-liner (assuming sliders have a value range of [0,5):
您可以为每个滑块上的每个位置指定一个从 1 到 1000000000 的不同二进制值
然后处理总和。
You could give each position on each slider a different binary value from 1 to 1000000000
and then work with the sum.
是的,但不是以正常的方式。您将不得不使用 switch 作为关闭。
前任:-
Yeah, But not in a normal way. You will have to use switch as closure.
ex:-
我是这样做的:
I did it like this:
与上述串联方法相同,但更有品味 - 并且可能更有效? - 对于数字,您可以使用简单的双射(具体来说,我将其与几个正整数一起使用,并且我对浮点/实数双射没有任何信心)
如果您只处理数字,则可以使用简单的双射
例如:Cantor 配对函数 从 N² 到 N
那么
对于任何整数的元组来说,同样应该是安全可行的长度 n,因为 Z^n 与 N 具有相同的基数。
In the same vein as above-mentioned concatenation methods but more tastefully - and likely efficiently ? - for numbers you could use a simple bijection (specifically I used this with couples of positive integers, and I wouldn't have any confidence about floating point / real numbers bijections)
If you are only dealing with numbers, you could use simple bijections
For example : Cantor pairing function from N² to N
Then
The same should be safely feasible for tuples of integers of any length n since Z^n has the same cardinality as N.
这是我将采取的方法,我认为它比其他建议更清晰:
这可以让您将所有程序逻辑良好地组织成可以重用的离散函数,而不是处理条件,做正确的事情就是简单的字典查找。带有
&&
的语句只是检查以确保第一项已定义,以避免未定义错误,返回值是逻辑结果,但您可以直接返回函数,具体取决于什么对你的申请有意义。Here's the approach I would take, which I think is cleaner than the other proposals:
This lets you keep all your program logic well organized into discrete functions that can be reused and instead of dealing with conditionals doing the right thing is a simple dictionary lookup. The statement with the
&&
is just checking to ensure the first item is defined to avoid an error on undefined, and the return value is the result of the logic but you could return the function directly depending on what makes sense in your application.