当前位置:网站首页>Typescript from getting started to mastering (19) enumeration types
Typescript from getting started to mastering (19) enumeration types
2022-07-29 03:44:00 【Haha, APE】
Write a little demo
Random number drinking game , We shake a few at random and drink a few drinks
Write a random number Random generation 0-6
const RandomNumber = Math.round(Math.random()*6)
Write another way
function Drink(num:number):string{
if(num==0){
return " No drinking "
}else if(num==1){
return " Have a drink "
}else if(num==2){
return " Have two drinks "
}else if(num==3){
return " Drink three cups "
}else if(num==4){
return " Drink four cups "
}else if(num==5){
return " Drink five cups "
}else if(num==6){
return " Drink six cups "
}
}
console.log(Drink(RandomNumber))
This simply completes our requirements , But the above writing is the writing of beginners .
Let's transform , The following code
const RandomNumber = Math.round(Math.random()*6)
const Status ={
zero:0,
one:1,
two:2,
three:3,
four:4,
five:5,
six:6,
}
function Drink(num:number):string{
if(num==Status.zero){
return " No drinking "
}else if(num==Status.one){
return " Have a drink "
}else if(num==Status.two){
return " Have two drinks "
}else if(num==Status.three){
return " Drink three cups "
}else if(num==Status.four){
return " Drink four cups "
}else if(num==Status.five){
return " Drink five cups "
}else if(num==Status.six){
return " Drink six cups "
}
}
console.log(Drink(RandomNumber))
But the top is not high enough , Advanced writing will use our enumeration types enum, The following code
const RandomNumber = Math.round(Math.random()*6)
enum Status {
zero,
one,
two,
three,
four,
five,
six
}
function Drink(num:number):string{
if(num==Status.zero){
return " No drinking "
}else if(num==Status.one){
return " Have a drink "
}else if(num==Status.two){
return " Have two drinks "
}else if(num==Status.three){
return " Drink three cups "
}else if(num==Status.four){
return " Drink four cups "
}else if(num==Status.five){
return " Drink five cups "
}else if(num==Status.six){
return " Drink six cups "
}
}
console.log(Drink(RandomNumber))
The corresponding value of enumeration type
At this point, we pass a value 1, Will be output Have a drink
console.log(Drink(1));
// Print the results : Have a drink
It looks amazing , This is because enumeration types have corresponding numeric values , The default is 0 At the beginning . We just use console.log() You can see that .
console.log(Status.zero); // The result is :0
console.log(Status.one);// The result is :1
console.log(Status.two);// The result is :2
console.log(Status.three);// The result is :3
console.log(Status.four);// The result is :4
console.log(Status.five);// The result is :5
console.log(Status.six);// The result is :6
To see the result is 0,1,2,3,4,5,6. At this time, I don't want to default from 0 Start , I want to start with 10 Start . It can be written like this
enum Status {
zero = 10,
one,
two,
three,
four,
five,
six
}
We can Enumeration is inverted by subscript Check whether it is from 10 At the beginning
console.log(Status.zero)
console.log(Status.one)
console.log(Status[10])
console.log(Status[11])
The output result is : 1011zeroone
边栏推荐
- Mathematical modeling -- analytic hierarchy process model
- Why does the 20 bit address bus determine the storage space of 1MB
- HDU multi School Game 2 1011 DOS card
- Realize multi-level linkage through recursion
- With more than 5 years of work experience and a salary of 15K, would you accept it if you were me?
- Why do programmers so "dislike" the trunk development mode?
- Malloc C language
- 代码 ~ 隐藏或禁用状态栏和虚拟按键
- Simple understanding of Poe and UPS Technology
- Learn exkmp again (exkmp template)
猜你喜欢

(2022杭电多校三)1011-Link is as bear(思维+线性基)

Beijing post network research 2015 problem2

(newcoder 15079) irrelevant (inclusion exclusion principle)

How to understand "page storage management scheme"
![MOS tube - rapid recovery application notes (II) [parameters and applications]](/img/54/eb040a51304192def8cfb360c7c213.png)
MOS tube - rapid recovery application notes (II) [parameters and applications]

Excel splicing database statement

新零售O2O 电商模式解析

消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!

(newcoder 15079)无关(容斥原理)

Instance setup flask service (simple version)
随机推荐
ROS-Errror:Did you forget to specify generate_ messages(DEPENDENCIES ...)?
Connect with third-party QQ login
Realize multi-level linkage through recursion
JS regular expression finds the number of times a character (string) appears (one line of code)
RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption
Sleuth+zipkin to track distributed service links
Li Kou daily question - day 44 -205. Isomorphic string
Instance setup flask service (simple version)
Cloudera manager platform fault repair record
(2022 Hangdian multi school III) 1011 link is as bear (thinking + linear basis)
Multi level wavelet CNN for image restoration
Excel拼接数据库语句
Why do many programmers hate pair programming?
(2022 Hangdian multi school III) 1002 boss rush (pressure dp+ dichotomy)
Deep into C language (3) -- input and output stream of C
RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密
Flask framework operation database_ Add, delete, modify and query statements
Suffix automata (SAM) board from Jly
Rdkit II: use rdkit screening to screen 2D pharmacophores of chemical small molecules
Simple understanding of CDN, SDN and QoS