当前位置:网站首页>typescript枚举
typescript枚举
2022-07-01 22:37:00 【豆瓣酱,】
一,前言
1.enum是在typescript中新增的合法类型,是对JavaScript标准数据类型的一个补充。
2.enum的主要作用是可以为一组限定范围内数值赋予友好的名字。
二,enum介绍
1.enmu的基本形式如下
enum Animals {
Dog,
Cat,
Pig,
}
2.根据值和定义形式主要可以分为三种类型:数字枚举,字符串枚举,常量枚举
三,数字枚举
1.枚举值为数字的为数字枚举
enum Animals {
Dog=1,
Cat=2,
Pig=3,
}
2.默认情况下,若不手动为枚举成员赋值,则会被定义成数字枚举,且从编号0开始递增为枚举成员赋值
enum Animals {
Dog,
Cat,
Pig,
}
console.log(Animals.Dog) //0
console.log(Animals.Cat) //1
console.log(Animals.Pig) //2
3.我们可以为其中的一个或多个手动赋值,之后的顺位按赋值的数值递增
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.枚举项有两种类型:常数项和计算所得项,前面所说的都是常数项,需要注意的是,当其中一项为计算所得项时,那么它的下位一定要赋予初始值,否则会报错,由于使用计算所得项,自动计算下位失效了。
enum Animals {
Dog,
Cow=Math.random(), //Cow为常数项
Cat=2, //下一位需要赋予初始值,否则会报错
Pig,
}
二,字符串枚举
1.枚举值为字符串的为字符串枚举
enum Animals {
Dog="dog",
Cat="cat",
Pig="pig",
}
2.字符串枚举值要求每个字段的值都必须是字符串字面量,不能使用计算所得项
三,常数枚举
1.使用const enmu创建的枚举为常数枚举
const enum Animals {
Dog="dog",
Cat="cat",
Pig="pig",
}
2.常数枚举和普通枚举的区别就是常数枚举本身会在编译的时候被删除,只会在使用到的地方,拿到相应的值进行替换。
四,反向映射
1.当我们定义了枚举可以使用enmu.key或者enmu[key]来获取值
const enum Animals {
Dog="dog",
Cat="cat",
Pig="pig",
}
console.log(Animals["Dog"]) //dog
console.log(Animals.Cat) //cat
2.不仅如此我们可以根据值来获取枚举项的key
const enum Animals {
Dog,
Cat,
Pig,
}
console.log(Animals[0]) //Dog
console.log(Animals[2]) //Pig
五,枚举类型
1.枚举本身和枚举成员可以作为一种类型来声明变量
enum Animals {
Dog,
Cat,
Pig,
}
let test:Animals = Animals.Dog //枚举本身作为类型,只能赋值枚举里的枚举项
let test:Animals = 12 //声明了是Animals类型,赋值其他值会报错
let test:Animals.Dog = Animals.Dog //枚举成员作为类型,只能赋值相应的枚举值
let test:Animals.Dog = Animals.Cat //报错
边栏推荐
- A few minutes before work, I found out V-model and The difference between sync
- 测试人进阶技能:单元测试报告应用指南
- rviz打开后如何显示实时2D地图
- 建模和影视后期有什么关联?
- Timer和ScheduledThreadPoolExecutor的区别
- 力扣 710. 黑名单中的随机数
- ECMAScript 2022 was officially released. Have you heard about it?
- Happy number [fast and slow pointer of ring PROBLEMS]
- 若干互联网暴露面的收敛及处置建议
- RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
猜你喜欢
![Jerry's burning of upper version materials requires [chapter]](/img/65/fcd804e00dc08a2bd056e8e6493829.png)
Jerry's burning of upper version materials requires [chapter]
![[MySQL] basic use of explain and the function of each column](/img/d6/64f65ba21f5cda2c409477705f6a79.png)
[MySQL] basic use of explain and the function of each column

Multiple smart pointers

RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"

The online beggar function of Japanese shopping websites

思科--高可用和高可靠网络考试

Some abilities can't be learned from work. Look at this article, more than 90% of peers

Explain ThreadLocal in detail

【微服务|Sentinel】sentinel整合openfeign

Reprint CSDN article operation
随机推荐
91. (cesium chapter) cesium rocket launch simulation
有些能力,是工作中学不来的,看看这篇超过90%同行
2022年起重机司机(限桥式起重机)考试试题及模拟考试
Where can the courses purchased by CSDN be accessed
Contents of other parts of the map
CKS CKA CKAD 将终端更改为远程桌面
【小程序】通过scroll-view组件实现左右【滑动】列表
Cisco -- highly available and reliable network examination
[daily training] 66 add one-tenth
Turn -- the underlying debugging principle of GDB is so simple
Explain JMM in detail
Compare the version number [double pointer to intercept the string you want]
locust的使用
[daily training] 326 Power of 3
众昂矿业:发展以氟化工为主的特色化工产业具有先天优势
工作中非常重要的测试策略,你大概没注意过吧
You probably haven't noticed the very important testing strategy in your work
Some abilities can't be learned from work. Look at this article, more than 90% of peers
CKS CKA CKAD 将终端更改为远程桌面
通过Go语言创建CA与签发证书