当前位置:网站首页>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
边栏推荐
- 2022年R1快开门式压力容器操作考题及答案
- leetcode - 287. Find duplicates
- ESP自动下载电路设计
- win 10 mstsc连接 RemoteApp
- Explain ThreadLocal in detail
- Istio, ebpf and rsocket Broker: in depth study of service grid
- Simple interactive operation of electron learning (III)
- 日本购物网站的网络乞丐功能
- Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
- 玻璃马赛克
猜你喜欢
Jielizhi Bluetooth headset quality control and production skills [chapter]
【嵌入式系统课设】单个按键控制LED灯
CADD课程学习(3)-- 靶点药物相互作用
思科考试--冗余网络
[MySQL] index classification
Reprint CSDN article operation
“35岁,公司老总,月薪2万送外卖“:时代抛弃你,连声再见都没有
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
Use 3DMAX to make a chess piece
flutter Unable to load asset: assets/images/888. png
随机推荐
Happy number [fast and slow pointer of ring PROBLEMS]
思科考试--路由的概念和配置考试
Mysql 5.7 实现 rank 排名
Explain the volatile keyword
实在RPA:银行数字化,业务流程自动化“一小步”,贷款审核效率“一大步”
A few minutes before work, I found out V-model and The difference between sync
The online beggar function of Japanese shopping websites
ESP自动下载电路设计
攻防演习防御体系构建之第三篇之建立实战化的安全体系
会声会影2022智能、快速、简单的视频剪辑软件
Some abilities can't be learned from work. Look at this article, more than 90% of peers
OpenVINO 模型性能评估工具—DL Workbench
AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络
Quantifiers of regular series
plain framework的实际应用和扩展
CADD course learning (3) -- target drug interaction
CKS CKA CKAD 将终端更改为远程桌面
Explain ThreadLocal in detail
Turn -- the underlying debugging principle of GDB is so simple
Unable to climb hill sort, directly insert sort