做者:Dr. Axel Rauschmayer翻译:疯狂的技术宅html
原文:https://2ality.com/2020/01/en...前端
未经容许严禁转载git
在本文中,咱们将会研究在 JavaScript 中实现基于类的枚举模式。还会研究一下 Enumify 这个可以帮助咱们使用枚举模式的库。程序员
枚举是由一组值组成的类型。例如 TypeScript 中有内置的枚举,咱们能够经过它们来定义本身的布尔类型:github
enum MyBoolean { false, true, }
或者能够定义本身的颜色类型:面试
enum Color { red, orange, yellow, green, blue, purple, }
这段 TypeScript 代码会被编译为如下 JavaScript 代码(省略了一些详细信息,以便于理解):segmentfault
const Color = { red: 0, orange: 1, yellow: 2, green: 3, blue: 4, purple: 5, };
这种实现有几个问题:安全
Color.red
,是看不到它的名称的。1
可能会误认为 Color.green
,反之亦然。Color
的元素。用普通 JavaScript,咱们能够经过使用字符串而不是数字做为枚举值来解决问题 1:服务器
const Color = { red: 'red', orange: 'orange', yellow: 'yellow', green: 'green', blue: 'blue', purple: 'purple', }
若是咱们用符号做为枚举值,还可以得到类型安全性:微信
const Color = { red: Symbol('red'), orange: Symbol('orange'), yellow: Symbol('yellow'), green: Symbol('green'), blue: Symbol('blue'), purple: Symbol('purple'), } assert.equal( String(Color.red), 'Symbol(red)');
符号存在的一个问题是须要将它们明确转换为字符串,而不能强制转换(例如,经过 +
或内部模板文字):
assert.throws( () => console.log('Color: '+Color.red), /^TypeError: Cannot convert a Symbol value to a string$/ );
尽管能够测试成员资格,但这并不简单:
function isMember(theEnum, value) { return Object.values(theEnum).includes(value); } assert.equal(isMember(Color, Color.blue), true); assert.equal(isMember(Color, 'blue'), false);
经过对枚举使用自定义类可使咱们进行成员资格测试,并在枚举值方面具备更大的灵活性:
class Color { static red = new Color('red'); static orange = new Color('orange'); static yellow = new Color('yellow'); static green = new Color('green'); static blue = new Color('blue'); static purple = new Color('purple'); constructor(name) { this.name = name; } toString() { return `Color.${this.name}`; } }
我把这种用类做为枚举的方式称为“枚举模式”。它受到 Java 中对枚举实现的启发。
输出:
console.log('Color: '+Color.red); // Output: // 'Color: Color.red'
成员资格测试:
assert.equal( Color.green instanceof Color, true);
Enumify 是一个可以帮助咱们使用枚举模式的库。它的用法以下:
class Color extends Enumify { static red = new Color(); static orange = new Color(); static yellow = new Color(); static green = new Color(); static blue = new Color(); static purple = new Color(); static _ = this.closeEnum(); }
Enumify 可以把多个实例属性添加到枚举值中:
assert.equal( Color.red.enumKey, 'red'); assert.equal( Color.red.enumOrdinal, 0);
用 Enumify 实现 .toStrin()
:
assert.equal( 'Color: ' + Color.red, // .toString() 'Color: Color.red');
Enumify 设置了两个静态属性– .enumKeys
和 .enumValues
:
assert.deepEqual( Color.enumKeys, ['red', 'orange', 'yellow', 'green', 'blue', 'purple']); assert.deepEqual( Color.enumValues, [ Color.red, Color.orange, Color.yellow, Color.green, Color.blue, Color.purple]);
它提供了可继承的静态方法 .enumValueOf()
:
assert.equal( Color.enumValueOf('yellow'), Color.yellow);
它实现了可继承的可迭代性:
for (const c of Color) { console.log('Color: ' + c); } // Output: // 'Color: Color.red' // 'Color: Color.orange' // 'Color: Color.yellow' // 'Color: Color.green' // 'Color: Color.blue' // 'Color: Color.purple'
class Weekday extends Enumify { static monday = new Weekday(true); static tuesday = new Weekday(true); static wednesday = new Weekday(true); static thursday = new Weekday(true); static friday = new Weekday(true); static saturday = new Weekday(false); static sunday = new Weekday(false); static _ = this.closeEnum(); constructor(isWorkDay) { super(); this.isWorkDay = isWorkDay; } } assert.equal(Weekday.sunday.isWorkDay, false); assert.equal(Weekday.wednesday.isWorkDay, true);
枚举模式也有其缺点:一般在建立枚举时不能引用其余的枚举(由于这些枚举可能还不存在)。解决方法是,能够经过如下函数在外部实现辅助函数:
class Weekday extends Enumify { static monday = new Weekday(); static tuesday = new Weekday(); static wednesday = new Weekday(); static thursday = new Weekday(); static friday = new Weekday(); static saturday = new Weekday(); static sunday = new Weekday(); static _ = this.closeEnum(); } function nextDay(weekday) { switch (weekday) { case Weekday.monday: return Weekday.tuesday; case Weekday.tuesday: return Weekday.wednesday; case Weekday.wednesday: return Weekday.thursday; case Weekday.thursday: return Weekday.friday; case Weekday.friday: return Weekday.saturday; case Weekday.saturday: return Weekday.sunday; case Weekday.sunday: return Weekday.monday; default: throw new Error(); } }
另外一个解决在声明枚举时没法使用其余枚举的方法是经过 getter 延迟访问同级的值:
class Weekday extends Enumify { static monday = new Weekday({ get nextDay() { return Weekday.tuesday } }); static tuesday = new Weekday({ get nextDay() { return Weekday.wednesday } }); static wednesday = new Weekday({ get nextDay() { return Weekday.thursday } }); static thursday = new Weekday({ get nextDay() { return Weekday.friday } }); static friday = new Weekday({ get nextDay() { return Weekday.saturday } }); static saturday = new Weekday({ get nextDay() { return Weekday.sunday } }); static sunday = new Weekday({ get nextDay() { return Weekday.monday } }); static _ = this.closeEnum(); constructor(props) { super(); Object.defineProperties( this, Object.getOwnPropertyDescriptors(props)); } } assert.equal( Weekday.friday.nextDay, Weekday.saturday); assert.equal( Weekday.sunday.nextDay, Weekday.monday);
getter 传递给对象内部的构造函数。构造函数经过 Object.defineProperties() 和 Object.getOwnPropertyDescriptors()
将它们复制到当前实例。可是咱们不能在这里使用 Object.assign()
,由于它没法复制 getter 和其余方法。
在下面的例子中实现了一个状态机。咱们将属性(包括方法)传递给构造函数,构造函数再将其复制到当前实例中。
class State extends Enumify { static start = new State({ done: false, accept(x) { if (x === '1') { return State.one; } else { return State.start; } }, }); static one = new State({ done: false, accept(x) { if (x === '1') { return State.two; } else { return State.start; } }, }); static two = new State({ done: false, accept(x) { if (x === '1') { return State.three; } else { return State.start; } }, }); static three = new State({ done: true, }); static _ = this.closeEnum(); constructor(props) { super(); Object.defineProperties( this, Object.getOwnPropertyDescriptors(props)); } } function run(state, inputString) { for (const ch of inputString) { if (state.done) { break; } state = state.accept(ch); console.log(`${ch} --> ${state}`); } }
状态机检测字符串中是否存在连续的三个 1
的序列:
run(State.start, '01011100'); // Output: // '0 --> State.start' // '1 --> State.one' // '0 --> State.start' // '1 --> State.one' // '1 --> State.two' // '1 --> State.three'
有时咱们须要枚举值是数字(例如,用于表示标志)或字符串(用于与 HTTP 头中的值进行比较)。能够经过枚举来实现。例如:
class Mode extends Enumify { static user_r = new Mode(0b100000000); static user_w = new Mode(0b010000000); static user_x = new Mode(0b001000000); static group_r = new Mode(0b000100000); static group_w = new Mode(0b000010000); static group_x = new Mode(0b000001000); static all_r = new Mode(0b000000100); static all_w = new Mode(0b000000010); static all_x = new Mode(0b000000001); static _ = this.closeEnum(); constructor(n) { super(); this.n = n; } } assert.equal( Mode.user_r.n | Mode.user_w.n | Mode.user_x.n | Mode.group_r.n | Mode.group_x.n | Mode.all_r.n | Mode.all_x.n, 0o755); assert.equal( Mode.user_r.n | Mode.user_w.n | Mode.user_x.n | Mode.group_r.n, 0o740);