有静态方法的枚举

该文章发布于 ,归类于 Javascript

可以使用enumnamespace为枚举添加静态方法。

enum Weekday {
  Monday,
  Tuseday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
}

namespace Weekday {
  export function isBussinessDay(day: Weekday) {
    switch (day) {
      case Weekday.Saturday:
      case Weekday.Sunday:
        return false
      default:
        return true
    }
  }
}

调用如下

console.log(Weekday.isBussinessDay(Weekday.Monday))

// 输出:true

相关文章