当前位置:网站首页>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
边栏推荐
- [机缘参悟-35]:鬼谷子-飞箝篇-远程连接、远程控制与远程测试之术
- Using emqx cloud to realize one machine one secret verification of IOT devices
- Jielizhi Bluetooth headset quality control and production skills [chapter]
- 【Kotlin 第三方 】coil koltin协程图片加载库Coil类似Glide的图片加载第三方
- Demo program implementation of QT version Huarui camera
- Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
- What is the relationship between modeling and later film and television?
- Cisco -- an external tool for WAN's concept examination
- 想请教股票开户要认识谁?在线开户是安全么?
- Turn -- the underlying debugging principle of GDB is so simple
猜你喜欢

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

I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction

使用3DMax制作一个象棋棋子
![[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

软件测试之「 性能测试」总结,新手上路必会知识点
![Happy number [fast and slow pointer of ring PROBLEMS]](/img/37/5c94b9b062a54067a50918f94e61ea.png)
Happy number [fast and slow pointer of ring PROBLEMS]

CADD course learning (3) -- target drug interaction

Groups and ranges of regular series

实在RPA:银行数字化,业务流程自动化“一小步”,贷款审核效率“一大步”

window安装wsl(二)
随机推荐
openresty 负载均衡
用JpaTransactionManager操作数据库事务
A few minutes before work, I found out V-model and The difference between sync
Mysql 5.7 实现 rank 排名
Some abilities can't be learned from work. Look at this article, more than 90% of peers
[target tracking] | single target tracking indicator
从第三次技术革命看企业应用三大开发趋势
Istio, ebpf and rsocket Broker: in depth study of service grid
工作中非常重要的测试策略,你大概没注意过吧
使用3DMax制作一个象棋棋子
众昂矿业:发展以氟化工为主的特色化工产业具有先天优势
通过Go语言创建CA与签发证书
关于游戏性能优化的一些感想
会声会影2022智能、快速、简单的视频剪辑软件
【微服务|Sentinel】sentinel整合openfeign
Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
Explain the volatile keyword
Jerry's burning of upper version materials requires [chapter]
想请教一下,证券开户选择哪个证券比较好?手机开户是安全么?
MySQL -- index of InnoDB storage engine