当前位置:网站首页>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
边栏推荐
- Use 3DMAX to make a chess piece
- window10安装wsl(一)(WslRegisterDistribution ERROR)
- window安装wsl(二)
- 想请教一下,证券开户选择哪个证券比较好?手机开户是安全么?
- OpenVINO 模型性能评估工具—DL Workbench
- 思科考试--路由的概念和配置考试
- Istio、eBPF 和 RSocket Broker:深入研究服务网格
- Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
- Convergence and disposal suggestions of some Internet exposure surfaces
- Tcpdump command usage details
猜你喜欢

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

Explain the use of locksupport in detail

You probably haven't noticed the very important testing strategy in your work

AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络
![Jielizhi Bluetooth headset quality control and production skills [chapter]](/img/ad/28e7461f8c5dc5c54a3f4da0c111ac.png)
Jielizhi Bluetooth headset quality control and production skills [chapter]

What is the relationship between modeling and later film and television?

Explain ThreadLocal in detail

Istio, ebpf and rsocket Broker: in depth study of service grid

Digital currency: far-reaching innovation

Quantifiers of regular series
随机推荐
软考信息系统项目管理师_整理的十大管理过程的简写帮助记忆背诵---软考高级之信息系统项目管理师054
微服务服务稳定性治理
Jielizhi Bluetooth headset quality control and production skills [chapter]
A few minutes before work, I found out V-model and The difference between sync
Jielizhi, production line assembly link [chapter]
会声会影2022智能、快速、简单的视频剪辑软件
What is the relationship between modeling and later film and television?
Explain the volatile keyword
openresty 负载均衡
数字峰会人气火爆,城链科技引发新一轮商业变革
plain framework的实际应用和扩展
Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
STM32F030F4驱动TIM1637数码管芯片
Cisco test -- the concept and configuration test of routing
CKS CKA ckad change terminal to remote desktop
window10安装wsl(一)(WslRegisterDistribution ERROR)
Istio、eBPF 和 RSocket Broker:深入研究服务网格
tcpdump命令使用详解
CKS CKA ckad change terminal to remote desktop
Multiple smart pointers