TypeScript 枚举(Enum)

刺骨的言语ヽ痛彻心扉 2023-07-24 08:40 91阅读 0赞

TypeScript 枚举(Enum)

本节介绍枚举类型的定义及其使用,需要定义一组相同主题的常量数据时,应该立即想到枚举类型。在学习过程中,需要注意枚举类型的正向映射和反向映射,可以通过编译后的 JavaScript 源码进行分析,为什么可以进行反向映射。

1. 慕课解释

使用枚举我们可以定义一些带名字的常量。TypeScript 支持数字的和基于字符串的枚举。

2. 定义及使用场景

枚举类型补充了 JavaScript 的设计不足,很多语言都拥有枚举类型。

当我们需要一组相同主题下的数据时,枚举类型就很有用了。

  1. enum Direction { Up, Down, Left, Right }
  2. enum Months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }
  3. enum Size { big = '大', medium = '中', small = '小' }
  4. enum Agency { province = 1, city = 2, district = 3 }

代码解释:

代码中通过枚举类型分别声明了:不同的 方向,一年内不同的 月份,一个商品的不同 尺寸属性,经销商的不同 级别,这样的一组常量数据,是在一个相同主题下的不同表示。

3. 数字枚举与字符串枚举

声明一个枚举类型,如果没有赋值,它们的值默认为数字类型且从 0 开始累加:

  1. enum Months {
  2. Jan,
  3. Feb,
  4. Mar,
  5. Apr
  6. }
  7. Months.Jan === 0 // true
  8. Months.Feb === 1 // true
  9. Months.Mar === 2 // true
  10. Months.Apr === 3 // true

现实中月份是从 1 月开始的,那么只需要这样:

  1. // 从第一个数字赋值,往后依次累加
  2. enum Months {
  3. Jan = 1,
  4. Feb,
  5. Mar,
  6. Apr
  7. }
  8. Months.Jan === 1 // true
  9. Months.Feb === 2 // true
  10. Months.Mar === 3 // true
  11. Months.Apr === 4 // true

代码解释:

第 3 行,从属性 Jan 被赋值为 1 开始,后续的属性值依次累加。

枚举类型的值为字符串类型

案例演示

  1. enum TokenType {
  2. ACCESS = 'accessToken',
  3. REFRESH = 'refreshToken'
  4. }
  5. // 两种不同的取值写法
  6. console.log(TokenType.ACCESS === 'accessToken') // true
  7. console.log(TokenType['REFRESH'] === 'refreshToken') // true

运行案例点击 “运行案例” 可查看在线运行效果

代码解释: 枚举的取值,有 TokenType.ACCESSTokenType['ACCESS'] 这两种不同的写法,效果是相同的。

数字类型和字符串类型可以混合使用,但是不建议:

  1. enum BooleanLikeHeterogeneousEnum {
  2. No = 0,
  3. Yes = "YES",
  4. }

4. 计算常量成员

枚举类型的值可以是一个简单的计算表达式:

案例演示

  1. enum Calculate {
  2. a,
  3. b,
  4. expired = 60 * 60 * 24,
  5. length = 'imooc'.length,
  6. plus = 'hello ' + 'world'
  7. }
  8. console.log(Calculate.expired) // 86400
  9. console.log(Calculate.length) // 5
  10. console.log(Calculate.plus) // hello world

运行案例点击 “运行案例” 可查看在线运行效果

Tips:

  • 计算结果必须为常量。
  • 计算项必须放在最后。

5. 反向映射

所谓的反向映射就是指枚举的取值,不但可以正向的 Months.Jan 这样取值,也可以反向的 Months[1] 这样取值。

  1. enum Months {
  2. Jan = 1,
  3. Feb,
  4. Mar,
  5. Apr
  6. }

将上面的代码进行编译,查看编译后的 JavaScript 代码:

  1. 'use strict'
  2. var Months;
  3. (function (Months) {
  4. Months[Months['Jan'] = 1] = 'Jan'
  5. Months[Months['Feb'] = 2] = 'Feb'
  6. Months[Months['Mar'] = 3] = 'Mar'
  7. Months[Months['Apr'] = 4] = 'Apr'
  8. })(Months || (Months = {}))

通过查看编译后的代码,可以得出:

  1. console.log(Months.Mar === 3) // true
  2. // 那么反过来能取到 Months[3] 的值吗?
  3. console.log(Months[3]) // 'Mar'
  4. // 所以
  5. console.log(Months.Mar === 3) // true
  6. console.log(Months[3] === 'Mar') // true

Tips:

  1. 字符串枚举成员不会生成反向映射。
  2. 枚举类型被编译成一个对象,它包含了正向映射( name -> value)和反向映射( value -> name)。

6. const 枚举

在枚举上使用 const 修饰符:

  1. enum Months {
  2. Jan = 1,
  3. Feb,
  4. Mar,
  5. Apr
  6. }
  7. const month = Months.Mar

查看一下编译后的内容:

  1. 'use strict'
  2. const month = 3 /* Mar */

发现枚举类型应该编译出的对象没有了,只剩下 month 常量。这就是使用 const 关键字声明枚举的作用。因为变量 month 已经使用过枚举类型,在编译阶段 TypeScript 就将枚举类型抹去,这也是性能提升的一种方案。

7. 枚举合并

分开声明名称相同的枚举类型,会自动合并:

案例演示

  1. enum Months {
  2. Jan = 1,
  3. Feb,
  4. Mar,
  5. Apr
  6. }
  7. enum Months {
  8. May = 5,
  9. Jun
  10. }
  11. console.log(Months.Apr) // 4
  12. console.log(Months.Jun) // 6

运行案例点击 “运行案例” 可查看在线运行效果

编译后的 JavaScript 代码:

  1. 'use strict'
  2. var Months;
  3. (function (Months) {
  4. Months[Months['Jan'] = 1] = 'Jan'
  5. Months[Months['Feb'] = 2] = 'Feb'
  6. Months[Months['Mar'] = 3] = 'Mar'
  7. Months[Months['Apr'] = 4] = 'Apr'
  8. })(Months || (Months = {}));
  9. (function (Months) {
  10. Months[Months['May'] = 5] = 'May'
  11. Months[Months['Jun'] = 6] = 'Jun'
  12. })(Months || (Months = {}))
  13. console.log(Months.Apr) // 4
  14. console.log(Months.Jun) // 6

8. 小结

通过本节的介绍需要知道:

  • 通过关键字 enum 来声明枚举类型。
  • TypeScript 仅支持基于数字和字符串的枚举。
  • 通过枚举类型编译后的结果,了解到其本质上就是 JavaScript 对象。

发表评论

表情:
评论列表 (有 0 条评论,91人围观)

还没有评论,来说两句吧...

相关阅读

    相关 (enum)

    前言 实际上,枚举类型是特殊的类,和C语言C++中的枚举不太一样,下面我们做详细说明。关于枚举类型有一个单独的设计模式:即单例设计模式。单例类是一个类只有一个实例,那么多

    相关 enum

    枚举类型enum C++中,枚举类型(enumeration) 是一个被命名的整型常量的集合。和类一样,每个枚举类型定义了一种新的类型。枚举属于字面值常量类型。C++包含

    相关 类(enum

    【1】什么是枚举类?     实例(也叫对象)有限且固定不变的类,在Java里被称为枚举类。     例如,季节类,它只有4个实例(春、夏、秋、冬),并且这4个实例不