当前位置:网站首页>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)
> 吴哲:平常心,平常心!--《士兵突击》
边栏推荐
- [software analysis] iterative explanation of software analysis, design and modeling
- 融云通信解决方案 破解企业沟通痛点
- Class extension and optional type extension of dart
- ctfshow XSS
- stm32F407-------LCD
- stm32F407-------时钟系统(SystemInit时钟初始化、Systick滴答定时器)
- 关联线探究,如何连接流程图的两个节点
- MySQL connection query is easy to understand
- The picture display on the left of the two column layout is determined by the content height on the right
- Stm32f407------- external interrupt
猜你喜欢

Stm32f407------- external interrupt

ctfshow XSS

Auto encoder

【软件分析】软件分析、设计与建模迭代式详解

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

融云通信解决方案 破解企业沟通痛点

PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报

Chapter IV memory management exercise

Stm32f407 ------ running lamp and buzzer

What pitfalls should be avoided in the job interview for the operation post in 2022?
随机推荐
Stm32f407 ------ clock system (systeminit clock initialization, systick tick timer)
Machine learning 4-dimension reduction technology
Non scientific class! The road of self-study!
Stm32f407------- external interrupt
Association line exploration, how to connect the two nodes of the flow chart
At the end of June, how many people in Kangkang are ready to change jobs
[machine learning] numerical analysis 02 -- finding roots of arbitrary equations
Junior, it's not easy!
入行数字IC验证后会做些什么?
[conception de la machine d'état] Moore, Mealy State Machine, Three - stage, Two - stage, one - stage State Machine Writing Specification
Is it safe and reliable to open a securities account in Yixue school?
[数学建模]Matlab非线性规划之fmincon()函数
Stm32f407-------- NVIC interrupt priority management
The picture display on the left of the two column layout is determined by the content height on the right
Is it safe to open a stock account on the Internet?
Behaviortree in ros2
MySQL connection query is easy to understand
【狀態機設計】Moore、Mealy狀態機、三段式、二段式、一段式狀態機書寫規範
Blue Bridge Cup top ten common heaven level skill - breath of water The type of one recursion
window10 phpstudy 安装redis扩展