ts的枚举类型简化if else if判断

一.枚举的定义方法

// 定义方法
enum name {name1, name2, name3, ...}
二.枚举的使用
enum Sex {
  MALE,
  FEMALE,
  UNKNOWN
}

let member_sex:Sex = Sex.FEMALE;
console.log(member_sex);
// console.log(Sex[member_sex]); // FEMALE
console.log(checkSex(member_sex)) // 女

function checkSex(sex: Sex) {
  let result: string = '';
  switch (sex) {
    case Sex.MALE:
      result = '男';
      break;
    case Sex.FEMALE:
      result = '女';
      break;
    case Sex.UNKNOWN:
      result = '未知';
      break;
    default:
      break;
  }
  return result;
}

  

posted @ 2019-12-29 17:03  realman?  阅读(2230)  评论(0编辑  收藏  举报