javascript开关/案例:类型比较? [英] javascript switch/case : are types compared?
本文介绍了javascript开关/案例:类型比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
javascript中的switch / case语句是比较类型还是仅比较值?
Does a switch/case statement in javascript compare types or only values?
换句话说,当我有以下代码时:
In other words, when I have the following code:
switch (variable)
{
case "0": [...] break;
case "1": [...] break;
default: [...] break;
}
相当于
if ( variable == "0" )
{
[...]
}
else if ( variable == "1" )
{
[...]
}
else
{
[...]
}
或
if ( variable === "0" )
{
[...]
}
else if ( variable === "1" )
{
[...]
}
else
{
[...]
}
编辑:有没有办法强制一次比较值和类型?
edit: is there a way to force compare values and types at once?
推荐答案
是的,比较类型。
如果输入等于===运算符定义的clauseSelector ,
然后设置为true。
If input is equal to clauseSelector as defined by the === operator, then set found to true.
ECMA-262 ,第95页。
这篇关于javascript开关/案例:类型比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文