959930
遭了, 这是心动的感觉
给不使用typescript的同学附上一个es6实现, 对外使用起来感受是一样的, 并且会有vscode智能提示
class Enum {
constructor(map) {
this.map = new Map(map)
}
getNames() {
return Object.keys(this).filter(key => key !== 'map')
}
getTexts() {
return Array.from(this.map.values())
}
getTextByName(name) {
return this.map.get(this[name])
}
getTextByValue(value) {
return this.map.get(value)
}
}
module.exports = {
Gender: new class extends Enum {
constructor() {
super([
[0, '未知'],
[1, '男性'],
[2, '女性'],
])
this.Unknow = 0
this.Male = 1
this.Female = 2
}
}()
}