有静态方法的枚举
可以使用enum
和namespace
为枚举添加静态方法。
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
如果您觉得本文对您有用,欢迎捐赠或留言~
- 本博客所有文章除特别声明外,均可转载和分享,转载请注明出处!
- 本文地址:https://www.leevii.com/?p=1609