zoukankan      html  css  js  c++  java
  • 【转】[JavaScript] switchcase 的用法

    几个例子:

    function case1(num){
     
    switch(num){
      
    case 1:
       document.writeln(
    "show 1!!");
       
    break;
      
    case 2:
       document.writeln(
    "show 2!!");
       
    break;
      
    case 3:
       document.writeln(
    "show 3!!");
       
    break;
      
    default:
       document.writeln(
    "show others!!");
       
    break;
     }
    }

    function case2(num){
     
    switch(num){
      
    case 1:
       document.writeln(
    "show 1!!");
       
    //沒有break,所以會繼續執行case 2
      case 2:
       document.writeln(
    "show 2!!");
       
    break;
      
    case 3:
       document.writeln(
    "show 3!!");
       
    //沒有break,所以會繼續執行case 4
      case 4:
       document.writeln(
    "show 4!!");
       
    break;
      
    default:
       document.writeln(
    "show others!!");
       
    break;
     }
    }

    function case3(num){
     
    switch(num){
      
    case 1:
      
    case 2:
       
    //相當於if(num==1 || num==2)
       document.writeln("show 1 or 2!!");
       
    break;
      
    case 3:
      
    case 4:
       
    //相當於if(num==3 || num==4)
       document.writeln("show 3 or 4!!");
       
    break;
      
    default:
       
    //相當於else
       document.writeln("show others!!");
       
    break;
     }
    }

    function case4(num){
     
    switch(f(num)){
      
    case 1:
      
    case 2:
       
    //相當於if(num==1 || num==2)
       document.writeln("show 1 or 2!!");
       
    break;
      
    case 3:
      
    case 4:
       
    //相當於if(num==3 || num==4)
       document.writeln("show 3 or 4!!");
       
    break;
      
    default:
       
    //相當於else
       document.writeln("show others!!");
       
    break;
     }
    }

    function f(num){
     
    return num;
    }

    function case5(num){
     
    switch(num<=2){
      
    case true:
       document.writeln(
    "num <= 2");
       
    break;
      
    case false:
       document.writeln(
    "num > 2");
       
    break;
     }
    }

    JavaScript switch case 语句设置范围

    var x=1 
    switch(true){ 
    case x>0&&x<10
    alert(
    1);break
    case x>=10&&x<20
    alert(
    2);break
  • 相关阅读:
    github fork项目后,代码更新
    UIScrollView的用法,属性
    调整屏幕亮度,调整字体大小
    iOS UIFont 字体名字大全
    ios 6以后,UILabel全属性
    oc中的各种遍历(迭代)方法
    判断app是否是第一次启动
    ios 显示代码块(show the code snippet library)
    ios 添加动画的方法
    添加app第一次启动页面
  • 原文地址:https://www.cnblogs.com/luckylei66/p/1446980.html
Copyright © 2011-2022 走看看