switch (xx) { case xx: break ......}

ES6 的 switch 跟 if 差不多,当某个固定的变量或者属性会发生变化的时候使用。

注意:每个 case 完都要用 break(在函数中可以用return)终止switch,不然会穿透(当前成立的条件之后的代码(包括下面的case)都会执行)
例子

  let res = 2;
  let resetValue = (res) => {
    switch (res) {
      case 0:
        return "未上架";
      case 1:
        return "已上架";
      case 2:
        return "正在上架";
      default:
        return "--";
    };
  };
  console.log(resetValue(res))
posted @ 2019-09-16 14:06  真的想不出来  阅读(512)  评论(0编辑  收藏  举报