当前位置:网站首页>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 //报错
边栏推荐
- rviz打开后如何显示实时2D地图
- 软考信息系统项目管理师_整理的十大管理过程的简写帮助记忆背诵---软考高级之信息系统项目管理师054
- 2022年R1快开门式压力容器操作考题及答案
- 2022年起重机司机(限桥式起重机)考试试题及模拟考试
- Force buckle 710 Random numbers in the blacklist
- [kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party
- 工作中非常重要的测试策略,你大概没注意过吧
- Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
- 2022年危险化学品经营单位安全管理人员考试题及在线模拟考试
- 正则系列之组和范围(Groups and Ranges)
猜你喜欢
日本购物网站的网络乞丐功能
Map container
转载csdn文章操作
The online beggar function of Japanese shopping websites
CKS CKA CKAD 将终端更改为远程桌面
什么是马赛克?
Hide the creation and use of users
思科考试--冗余网络
Using emqx cloud to realize one machine one secret verification of IOT devices
RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
随机推荐
91. (cesium chapter) cesium rocket launch simulation
What class loading mechanisms does the JVM have?
数字峰会人气火爆,城链科技引发新一轮商业变革
CKS CKA CKAD 将终端更改为远程桌面
Turn -- bring it and use it: share a gadget for checking memory leaks
【微服务|Sentinel】SentinelResourceAspect详解
Treatment of insufficient space in the root partition of armbain system
The online beggar function of Japanese shopping websites
【Kotlin 第三方 】coil koltin协程图片加载库Coil类似Glide的图片加载第三方
想请教一下,证券开户选择哪个证券比较好?手机开户是安全么?
众昂矿业:发展以氟化工为主的特色化工产业具有先天优势
You probably haven't noticed the very important testing strategy in your work
Hide the creation and use of users
【小程序】通过scroll-view组件实现左右【滑动】列表
Cloud Vulnerability Global Database
日本购物网站的网络乞丐功能
js——arguments的使用
[机缘参悟-35]:鬼谷子-飞箝篇-远程连接、远程控制与远程测试之术
Cisco -- highly available and reliable network examination
Stimulate new kinetic energy and promote digital economy in multiple places