当前位置:网站首页>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
边栏推荐
- 1. Mx6u driver development-2-led driver
- 【C语言入门】ZZULIOJ 1031-1035
- Machine learning [numpy]
- Complexity analysis learning
- Excel splicing database statement
- Learn exkmp again (exkmp template)
- Ribbon principle analysis namedcontextfactory
- 深入C语言(4)——switch的定义与使用
- How fast does it take to implement a super simple language
- Raft protocol - process demonstration
猜你喜欢

What have I learned from 200 machine learning tools?

Solve the delay in opening the console of Google browser

Practical application cases of digital Twins - smart energy

Multi level wavelet CNN for image restoration

The latest second edition of comic novels, listening to books, three in one, complete source code / integrated visa free interface / building tutorials / with acquisition interface

Uni app internationalization

Excel拼接数据库语句

向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用

Ribbon principle analysis namedcontextfactory

实例搭建Flask服务(简易版)
随机推荐
What have I learned from 200 machine learning tools?
(2022 Hangdian multi school III) 1011 link is as bear (thinking + linear basis)
容斥原理
Data too long for column 'xxx' at row 1 solution
Complexity analysis learning
(2022 Hangdian multi school III) 1002 boss rush (pressure dp+ dichotomy)
Why do programmers so "dislike" the trunk development mode?
The difference between /g /m /i of JS regular expressions
(codeforce547)C-Mike and Foam(质因子+容斥原理)
How to judge stun protocol
Solve the delay in opening the console of Google browser
Excel拼接数据库语句
How fast does it take to implement a super simple language
Introduction and comparison of unicast, multicast (target broadcast, multicast), broadcast, flooding, flooding
3.1 common neural network layer (I) image correlation layer
Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc
Vs code must know and know 20 shortcut keys!
Shutter start white screen
数字孪生实际应用案例-智慧能源篇
Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment