当前位置:网站首页>TypeScript -- 第七节 枚举
TypeScript -- 第七节 枚举
2022-06-28 23:43:00 【Run Coder】
/* * 枚举 enumeration(enum),枚举(Enum)类型用于取值被限定在一定范围内的场景。 * 使用枚举我们可以定义一些带名字的常量。 使用枚举可以清晰地表达意图或创建一组有区别的用例。 * TypeScript支持数字的和基于字符串的枚举。 * * */
//数字枚举
enum NumDirection {
Up = 1,//不初始化,默认从0开始
Down,
Left,
Right
}
//使用枚举很简单:通过枚举的属性来访问枚举成员,和枚举的名字来访问枚举类型
console.log(NumDirection);//通过枚举的名字来访问枚举类型
console.log(NumDirection.Up);//通过枚举的属性来访问枚举成员
// 字符串枚举
//在字符串枚举里,每个成员都必须用字符串字面量,或另外一个字符串枚举成员进行初始化。
enum EnumString{
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
console.log(EnumString);//通过枚举的名字来访问枚举类型
console.log(EnumString.Up)//通过枚举的属性来访问枚举成员
//异构枚举(Heterogeneous enums)
//不建议创建混合数据类型的枚举 示例如下:
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES",
}
//枚举项有两种类型:常数项(constant member)和计算所得项(computed member)
//枚举--常量
//1.它是枚举的第一个成员且没有初始化器,这种情况下它被赋予值 0:
// E.X is constant:
enum E {
X }
//2.它不带有初始化器且它之前的枚举成员是一个 数字常量。
// 这种情况下,当前枚举成员的值为它上一个枚举成员的值加1。
// All enum members in 'E1' and 'E2' are constant.
enum E1 {
X, Y, Z }
enum E2 {
A = 1, B, C
}
/* * 3.枚举成员使用 常量枚举表达式初始化。 * 常数枚举表达式是TypeScript表达式的子集,它可以在编译阶段求值。 当一个表达式满足下面条件之一时,它就是一个常量枚举表达式: 0. 一个枚举表达式字面量(主要是字符串字面量或数字字面量) 1. 一个对之前定义的常量枚举成员的引用(可以是在不同的枚举类型中定义的) 2.带括号的常量枚举表达式 3.一元运算符 +, -, ~其中之一应用在了常量枚举表达式 4.常量枚举表达式做为二元运算符 +, -, *, /, %, <<, >>, >>>, &, |, ^的操作对象。 若常数枚举表达式求值后为 NaN或 Infinity,则会在编译阶段报错。 * * */
enum FileAccess {
// constant members
None,
Read = 1 << 1,//左移运算符
Write = 1 << 2,
ReadWrite = Read | Write,//位运算符
// computed member
G = "123".length,//计算所得项
}
console.log(FileAccess.G)
//联合枚举类型与枚举成员的类型
enum ShapeKind {
Circle,
Square,
}
interface Circle {
kind: ShapeKind.Circle;
radius: number;
}
interface Square {
kind: ShapeKind.Square;
sideLength: number;
}
let c: Circle = {
kind: ShapeKind.Circle,
// kind: ShapeKind.Square,//报错
// ~~~~~~~~~~~~~~~~ Error!
radius: 100,
};
console.log(c.kind);
//运行时的枚举
enum E3 {
X, Y, Z
}
function f(obj: {
X: number }) {
return obj.X;
}
// Works, since 'E' has a property named 'X' which is a number.
console.log(f(E3));
//反向映射
enum Enum {
A
}
let a = Enum.A;
let nameOfA = Enum[a]; // "A"
console.log(Enum.A);//0
console.log(nameOfA);//"A"
/* * const 常量枚举 * 常量枚举通过在枚举上使用 const修饰符来定义 * 常量枚举与普通枚举的区别是,它会在编译阶段被删除,并且不能包含计算成员。 * */
const enum EnumConst {
A = 1,
// B= 'abc'.length //常量枚举不允许包含计算成员。
}
//外部枚举
// 此处必须使用const, 不然declare 定义的类型只会用于编译时的检查,编译结果中会被删除
declare const enum Directions {
Up,
Down,
Left,
Right
}
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];
console.log(directions)
> 吴哲:平常心,平常心!--《士兵突击》
边栏推荐
- [matlab]函数定义与使用
- scrapy保存数据到excel:利用openpyxl创建多张表,设置Excel行数限制
- What is the lifecycle of automated testing?
- Chapter II Classic synchronous exercises
- 炒股开户万一免五是靠谱么,安全么
- stm32F407-------LCD
- 是使用local_setup.bash 还是 setup.bash
- Stm32f407------- external interrupt
- The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)
- Stm32f407 ------ serial (serial port) communication
猜你喜欢

Chapter IV memory management exercise

MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数
![SQL note 2 [MySQL]](/img/a4/f711173ce731d95860746e309b7d74.jpg)
SQL note 2 [MySQL]

VSCode里使用条件断点(基于GDB)

Stm32f407----- register address name mapping analysis

Association line exploration, how to connect the two nodes of the flow chart

Be on the list again! Know that Chuangyu was selected as one of the top 50 competitive enterprises in China's network security industry in 2022

stm32F407-------LCD

Chapter III processor scheduling exercise
![[API packet capturing in selenium automation] installation and configuration of browsermobproxy](/img/67/3e15b2191ee23a8c4453aad007651d.png)
[API packet capturing in selenium automation] installation and configuration of browsermobproxy
随机推荐
Three communication skills in software testing
Online yaml to JSON tool
油猴脚本学习
Flutter obtains the coordinate size of any element in the interface through globalkey
Behaviortree in ros2
IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
Auto encoder
Use conditional breakpoints in vscode (based on GDB)
pymysql.Error 获取错误码与具体错误信息
MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数
Stm32f407 ------ serial (serial port) communication
Implementation of dynamic timer for quartz
mysql-5.7.30-winx64免安装版下载安装教程
stm32F407-------电容触摸按键
Learn binary tree like this
What pitfalls should be avoided in the job interview for the operation post in 2022?
[machine learning] numerical analysis 02 -- finding roots of arbitrary equations
CMake教程(一)
融云通信解决方案 破解企业沟通痛点
Lock4j -- distributed locking Middleware -- use / instance