javascript开关/案例:类型比较? [英] javascript switch/case : are types compared?

查看:58
本文介绍了javascript开关/案例:类型比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在Javascript switch语句中假设严格比较是否安全?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆