做者:Dr. Axel Rauschmayerhtml
翻译:疯狂的技术宅前端
原文:2ality.com/2020/01/enu…git
未经容许严禁转载github
在本文中,咱们将会研究在 JavaScript 中实现基于类的枚举模式。还会研究一下 Enumify 这个可以帮助咱们使用枚举模式的库。前端工程化
枚举是由一组值组成的类型。例如 TypeScript 中有内置的枚举,咱们能够经过它们来定义本身的布尔类型:安全
enum MyBoolean {
false,
true,
}
复制代码
或者能够定义本身的颜色类型:函数
enum Color {
red,
orange,
yellow,
green,
blue,
purple,
}
复制代码
这段 TypeScript 代码会被编译为如下 JavaScript 代码(省略了一些详细信息,以便于理解):工具
const Color = {
red: 0,
orange: 1,
yellow: 2,
green: 3,
blue: 4,
purple: 5,
};
复制代码
这种实现有几个问题:测试
Color.red
,是看不到它的名称的。1
可能会误认为 Color.green
,反之亦然。Color
的元素。用普通 JavaScript,咱们能够经过使用字符串而不是数字做为枚举值来解决问题 1:ui
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);
复制代码