如何将 AS3 case 语句组织到一行上?
您好,我希望找出正确的语法,以便更好地组织 Flash AS3 中的 Case 语句。例如,下面的例子不是有 3 个 case 语句,我不能以某种方式将它组织成只有 1 个吗?
switch (refID)
{
case "TS_38":
pgBool = true;
break;
case "TS_37":
pgBool = true;
break;
case "TS_36":
pgBool = true;
break;
default:
pgBool = false;
//break;
}
我尝试了这个,但它在我的测试中不起作用:
switch (refID)
{
case "TS_38", "TS_37", "TS_36":
pgBool = true;
break;
default:
pgBool = false;
//break;
}
Hi I'm hoping to find out what the correct syntax is to better organize Case statements in Flash AS3. So below for example instead of having 3 case statements, couldn't I organize it somehow into just 1?
switch (refID)
{
case "TS_38":
pgBool = true;
break;
case "TS_37":
pgBool = true;
break;
case "TS_36":
pgBool = true;
break;
default:
pgBool = false;
//break;
}
I tried this, but it did not work in my test:
switch (refID)
{
case "TS_38", "TS_37", "TS_36":
pgBool = true;
break;
default:
pgBool = false;
//break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
}
Try this:
}
你可以这样做:
you can do this:
您还可以使用 if 语句而不是 switch,然后您可以使用 &&和||接线员:
我忍不住注意到...你们的两个案例是相同的。
You could also use an if statement instead of switch, then you can use && and || operators:
I can't help but notice...two of your cases are identical.
不知道您可能会遇到哪些其他情况,但根据您提供的示例,您可以使用以下命令完全删除 switch 语句:
Don't know what other cases you might come across but with the example you gave you could remove the switch statement entirely with:
请考虑在此示例中不使用 switch case。 Switch case 并不是编写代码的最短方法,它经常提示您编写冗余代码(您的示例是“糟糕代码的好例子”)。
您的代码所做的可以像这样编写:
或者
(如果要多次调用此函数,那么您肯定希望重用数组/对象)。
由于我已经看到这个答案被否决了几次,我觉得它需要更深入的解释。
首先,为什么使用哈希表比 switch-case 更好。
它是可扩展的。如果您将哈希表存储在必须做出决定的函数之外,则不需要更改做出决定的函数,您可以从表中添加或删除键 - 这更灵活并且更具可读性代码。它还允许运行时修改,而这对于 switch-case 来说是不可能的。
哈希表之所以被称为哈希表,是因为它对键进行哈希处理,确保存储在表中的值的访问时间是恒定的(通常非常小)。同时 switch-case 在 AS3 中被编码为简单的 if-else 子句链,这意味着 switch-case 越长,函数验证每个条件所需的时间就越长。虽然 switch-case 可以让您对测试条件的顺序进行一定的控制,但您很少可以事先知道哪个会更频繁地触发。哈希表会平等地对待所有条件。
这意味着,在实践中,永远没有理由更喜欢 switch-case 而不是 hash-table。出于同样的原因,某些语言(尤其是 Python)甚至没有这种构造。
Consider not using switch case in this example. Switch case isn't the shortest way to write code and it often times prompts you to write redundant code (your example is a "good example of bad code").
What your code does could've been written like so:
or
(most certainly you would like to reuse the array / object, if this function is to be called more then once).
As I've seen this answer been downvoted few times, I feel like it requires more in-depth explanation.
First of all, why using a hash-table is preferable to switch-case.
It is scalable. If you store the hash-table outside the function that has to make the decision, you don't need to change the function that makes the decision, you add or remove a key from the table - which is more flexible and allows for more readable code. It also allows runtime modifications, impossible with switch-case.
Hash-table is called so because it hashes the keys, ensuring that the access time of the value stored in the table is constant (and usually very small). At the same time switch-case in encoded in AS3 as a simple chain of if-else clauses, which means that the longer the switch case is, the more time it will take for the function to verify each condition. While switch-case gives you certain control over the order in which conditions are tested, you can rarely tell beforehand which will trigger more often. Hash-table would treat all conditions equally.
This means that, in practice, there is never a reason to prefer switch-case to hash-table. Some languages, like, notably, Python, don't even have this construct for the very same reasons.