lanzhi

你不知道的JavaScript(九)switch语句

在c/c++、java这些强类型的语言中switch语句的表达式和case分支中的条件值都只能是char类型或整数。JS的switch语句有些不同,它可以是JS中的任意一种类型,这一点有些朋友可能并没留意。

    <script type="text/javascript">
        var func = function(){};
        var expr = "hello";
        //alert string
        switch (expr)
        {
            case undefined:
                alert("undefined");
                break;
            case func:
                alert("function");
                break;
            case 100:
                alert("number");
                break;
            case "hello":
                alert("string");
                break;
        }
    </script>

case中的条件可以是string类型,number类型,undefined等等,甚至还可以是一个自定义的函数。

分类:

技术点:

相关文章:

  • 2021-10-19
  • 2021-09-17
  • 2021-10-29
  • 2021-10-19
  • 2021-11-19
  • 2021-10-19
  • 2021-10-18
  • 2019-08-29
猜你喜欢
  • 2021-10-19
  • 2020-10-27
  • 2021-10-19
  • 2021-10-19
  • 2021-10-19
  • 2021-10-19
相关资源
相似解决方案