当前位置:网站首页>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 //报错
边栏推荐
- Copy ‘XXXX‘ to effectively final temp variable
- Armbain系统根分区空间不足处理
- 实在RPA:银行数字化,业务流程自动化“一小步”,贷款审核效率“一大步”
- AirServer最新Win64位个人版投屏软件
- "35 years old, the boss of the company, with a monthly salary of 20000, give away takeout": the times abandoned you, not even saying goodbye
- Reprint CSDN article operation
- 玻璃马赛克
- 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
- Cloud Vulnerability Global Database
- 证券开户选哪个证券公司比较好,哪个更安全
猜你喜欢

YOGA27多维一体电脑,兼具出色外观与高端配置

Reprint CSDN article operation

数字化转型道阻且长,如何迈好关键的第一步

建模和影视后期有什么关联?

CKS CKA CKAD 将终端更改为远程桌面

2022年危险化学品经营单位安全管理人员考试题及在线模拟考试

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

Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
![[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

MySQL -- index of MyISAM storage engine
随机推荐
Cisco -- highly available and reliable network examination
Reprint CSDN article operation
Use and function of spark analyze command map join broadcast join
Demo program implementation of QT version Huarui camera
认识线程
CKS CKA CKAD 将终端更改为远程桌面
攻防演习防御体系构建之第三篇之建立实战化的安全体系
赵福全:短期解决保供,长期要打造安全、高效有韧性的供应链
[MySQL] index classification
[daily training] 66 add one-tenth
Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
Flink SQL command line connection yarn
Use of locust
Some thoughts on game performance optimization
"Trust machine" empowers development
Some abilities can't be learned from work. Look at this article, more than 90% of peers
CADD课程学习(3)-- 靶点药物相互作用
【微服务|Sentinel】sentinel整合openfeign
Multiple smart pointers