当前位置:网站首页>Typescript enumeration
Typescript enumeration
2022-07-01 23:14:00 【Bean paste,】
One , Preface
1.enum Is in typescript New legal types in , It's right JavaScript A supplement to standard data types .
2.enum Its main function is to give friendly names to a set of values within a limited range .
Two ,enum Introduce
1.enmu The basic form is as follows
enum Animals {
Dog,
Cat,
Pig,
}
2. According to the value and definition form, it can be divided into three types : Enumeration of numbers , String Enum , Constant enumeration
3、 ... and , Enumeration of numbers
1. Enumerations with numeric values are numeric enumerations
enum Animals {
Dog=1,
Cat=2,
Pig=3,
}
2. By default , If you do not assign values to enumeration members manually , Will be defined as numeric enumeration , And from No 0 Start incrementally assigning values to enumeration members
enum Animals {
Dog,
Cat,
Pig,
}
console.log(Animals.Dog) //0
console.log(Animals.Cat) //1
console.log(Animals.Pig) //2
3. We can for one or more of them Manual assignment , The subsequent sequence is incremented by the value assigned
enum Animals {
Dog,
Cow=5,
Cat=2,
Pig,
}
console.log(Animals.Dog) //0
console.log(Animals.Cow) //5
console.log(Animals.Cat) //2
console.log(Animals.Pig) //3
4. There are two types of enumerations : Constant term and Calculated item , All the above are constant terms , It should be noted that , When one of the items is the calculated item , Then its subordinate must be given the initial value , Otherwise, an error will be reported , Due to the use of calculated items , The automatic calculation of the lower position is invalid .
enum Animals {
Dog,
Cow=Math.random(), //Cow Constant term
Cat=2, // Next, you need to assign an initial value , Otherwise, an error will be reported
Pig,
}
Two , String Enum
1. If the enumeration value is string, it is string enumeration
enum Animals {
Dog="dog",
Cat="cat",
Pig="pig",
}
2. The string enumeration value requires that the value of each field must be a string literal , Calculated items cannot be used
3、 ... and , Constant enumeration
1. Use const enmu The enumeration created is a constant enumeration
const enum Animals {
Dog="dog",
Cat="cat",
Pig="pig",
}
2. The difference between constant enumeration and ordinary enumeration is that constant enumeration itself will be deleted during compilation , Only in the place of use , Get the corresponding value and replace .
Four , Reverse mapping
1. When we define enumeration, we can use enmu.key perhaps enmu[key] To get the value
const enum Animals {
Dog="dog",
Cat="cat",
Pig="pig",
}
console.log(Animals["Dog"]) //dog
console.log(Animals.Cat) //cat
2. Not only that, we can get the enumeration item according to the value key
const enum Animals {
Dog,
Cat,
Pig,
}
console.log(Animals[0]) //Dog
console.log(Animals[2]) //Pig
5、 ... and , Enumeration type
1. Enumeration itself and enumeration members can be used as a type to declare variables
enum Animals {
Dog,
Cat,
Pig,
}
let test:Animals = Animals.Dog // Enumeration itself as a type , Only the enumeration items in the enumeration can be assigned
let test:Animals = 12 // The statement is Animals type , Assigning other values will result in an error
let test:Animals.Dog = Animals.Dog // Enumerate members as types , Only the corresponding enumeration value can be assigned
let test:Animals.Dog = Animals.Cat // Report errors
边栏推荐
- Data enhancement of semi supervised learning
- 正则系列之组和范围(Groups and Ranges)
- Jerry's burning of upper version materials requires [chapter]
- Explain JMM in detail
- Jielizhi, production line assembly link [chapter]
- 玻璃马赛克
- Cisco exam -- redundant network
- 【嵌入式系统课设】单个按键控制LED灯
- CKS CKA CKAD 将终端更改为远程桌面
- leetcode - 287. Find duplicates
猜你喜欢
![Jielizhi, production line assembly link [chapter]](/img/84/73637de7ba334c5ac48895a7e82575.png)
Jielizhi, production line assembly link [chapter]

Explain JMM in detail
![[applet] realize the left and right [sliding] list through the scroll view component](/img/18/b1b4e9923782856143721dad84cbab.png)
[applet] realize the left and right [sliding] list through the scroll view component

Réimpression de l'article csdn

Genicam gentl standard ver1.5 (4) Chapter 5 acquisition engine
![Happy number [fast and slow pointer of ring PROBLEMS]](/img/37/5c94b9b062a54067a50918f94e61ea.png)
Happy number [fast and slow pointer of ring PROBLEMS]

思科考试--冗余网络

思科--WAN 的概念考试外部工具

Istio、eBPF 和 RSocket Broker:深入研究服务网格

Demo program implementation of QT version Huarui camera
随机推荐
Multiple smart pointers
Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
Treatment of insufficient space in the root partition of armbain system
[MySQL] index classification
2022年起重机司机(限桥式起重机)考试试题及模拟考试
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
[JUC learning road day 9] barrier derivatives
Using emqx cloud to realize one machine one secret verification of IOT devices
Jielizhi, production line assembly link [chapter]
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
Redis~02 缓存:更新数据时如何保证MySQL和Redis中的数据一致性?
【微服务|Sentinel】@SentinelResource详解
工作中非常重要的测试策略,你大概没注意过吧
Detailed explanation of twenty common software testing methods (the most complete in History)
Redis~02 cache: how to ensure data consistency in MySQL and redis when updating data?
通过Go语言创建CA与签发证书
Summary of "performance testing" of software testing, novice will know the knowledge points on the road
马赛克后挡板是什么?
openresty 负载均衡
Tcpdump command usage details