当前位置:网站首页>Typescript -- Section 7 enumeration
Typescript -- Section 7 enumeration
2022-06-28 23:47:00 【Run Coder】
/* * enumeration enumeration(enum), enumeration (Enum) Type is used for scenarios where the value is limited to a certain range . * Using enumerations, we can define some named constants . Enumerations can be used to express intent clearly or to create a set of differentiated use cases . * TypeScript Supports numeric and string based enumeration . * * */
// Enumeration of numbers
enum NumDirection {
Up = 1,// Not initialized , The default from the 0 Start
Down,
Left,
Right
}
// Using enumerations is simple : Access enumeration members through enumerated properties , And the name of the enumeration to access the enumeration type
console.log(NumDirection);// Access enum types by enum names
console.log(NumDirection.Up);// Access enumeration members through enumerated properties
// String Enum
// In string enumeration , Each member must use string literal , Or another string enumeration member to initialize .
enum EnumString{
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
console.log(EnumString);// Access enum types by enum names
console.log(EnumString.Up)// Access enumeration members through enumerated properties
// Heterogeneous enumeration (Heterogeneous enums)
// It is not recommended to create enumerations of mixed data types Examples are as follows :
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES",
}
// There are two types of enumerations : Constant term (constant member) And the calculated term (computed member)
// enumeration -- Constant
//1. It is the first member of the enumeration and has no initializer , In this case, it is given a value 0:
// E.X is constant:
enum E {
X }
//2. It has no initializer and its previous enumeration member is a Digital constant .
// In this case , The value of the current enumeration member is the value of its previous enumeration member plus 1.
// All enum members in 'E1' and 'E2' are constant.
enum E1 {
X, Y, Z }
enum E2 {
A = 1, B, C
}
/* * 3. Enumeration members use Constant enumeration expression initialization . * The constant enumeration expression is TypeScript Subsets of expressions , It can be evaluated at compile time . When an expression satisfies one of the following conditions , It's just a constant enumeration expression : 0. An enumeration expression literal ( Mainly string literal or numeric literal ) 1. A reference to a previously defined constant enumeration member ( Can be defined in different enumeration types ) 2. Parenthesized constant enumeration expression 3. Unary operator +, -, ~ One of them is applied to constant enumeration expressions 4. Constant enumeration expressions are used as binary operators +, -, *, /, %, <<, >>, >>>, &, |, ^ Operation object of . If the constant enumeration expression evaluates to NaN or Infinity, An error will be reported at the compilation stage . * * */
enum FileAccess {
// constant members
None,
Read = 1 << 1,// Shift left operator
Write = 1 << 2,
ReadWrite = Read | Write,// An operator
// computed member
G = "123".length,// Calculated item
}
console.log(FileAccess.G)
// Union enum types and enum member types
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,// Report errors
// ~~~~~~~~~~~~~~~~ Error!
radius: 100,
};
console.log(c.kind);
// Runtime enumeration
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));
// Reverse mapping
enum Enum {
A
}
let a = Enum.A;
let nameOfA = Enum[a]; // "A"
console.log(Enum.A);//0
console.log(nameOfA);//"A"
/* * const Constant enumeration * Constant enumeration by using const To define * The difference between constant enumeration and ordinary enumeration is , It will be removed during the compilation phase , And can't contain calculated members . * */
const enum EnumConst {
A = 1,
// B= 'abc'.length // Constant enumerations are not allowed to contain calculated members .
}
// External enumeration
// You must use const, Otherwise declare The defined type will only be used for compile time checks , The compilation result will be deleted
declare const enum Directions {
Up,
Down,
Left,
Right
}
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];
console.log(directions)
> Wu Zhe : Common heart , Common heart !--《 Soldier assault 》
边栏推荐
- stm32F407-------GPIO输入实验
- Puma joins hands with 10ktf shop to launch its Web3 cooperation project with the largest scale so far
- Would like to ask, how to open a stock account? Is it safe to open an account online?
- pymysql.Error 获取错误码与具体错误信息
- PHP函数file_get_contents与操作系统的内存映射
- Association line exploration, how to connect the two nodes of the flow chart
- How many locks are added to an update statement? Take you to understand the underlying principles
- Form verification problem - El select (solution to automatically trigger verification on initialization page)
- MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数
- Yyds dry inventory solution sword finger offer: maximum sum of continuous subarrays (II)
猜你喜欢

stm32F407-------GPIO输入实验
![[buuctf.reverse] 131-135](/img/c2/b8b06c8191af2c75bf4ad5c82feaea.png)
[buuctf.reverse] 131-135

TypeScript -- 第二节:变量声明

Learning fuzzy from SQL injection to bypass the latest safe dog WAF

What pitfalls should be avoided in the job interview for the operation post in 2022?

Behaviortree in ros2
![[machine learning] numerical analysis 02 -- finding roots of arbitrary equations](/img/fd/ec82a50017e692ac90f6e8739b28d3.jpg)
[machine learning] numerical analysis 02 -- finding roots of arbitrary equations

Machine learning 6-decision tree

ERROR 1067 (42000): Invalid default value for ‘end_ time‘ Mysql

PHP uses endroid/qrcode QR code to generate, and Gd library generates sharing posters
随机推荐
Stm32f407 ------- RTC real time clock
Advice to friends
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
随笔记:重新认识 else if
Machine learning 4-dimension reduction technology
pymysql. Error get error code and specific error information
[machine learning] numerical analysis 02 -- finding roots of arbitrary equations
三个pwn题
Mysql-5.7.30-winx64 installation free download and installation tutorial
Hesitating root sound
When dialogfragment's onstop is completely invisible, call disass to exit the interface and report an error. Solution
Oil monkey script learning
Learning fuzzy from SQL injection to bypass the latest safe dog WAF
pymysql.Error 获取错误码与具体错误信息
Auto encoder
请问指南针股票软件可靠吗?在上面交易股票安全吗?
Stm32f407 ------ running lamp and buzzer
Finally, someone explained the cloud native architecture
【LeetCode】21. 合并两个有序链表 - Go 语言题解
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize