当前位置:网站首页>Simple use of enum type in TS

Simple use of enum type in TS

2022-06-26 06:23:00 One at the front

Why enumerate ?

Enumeration is just TypeScript A useful way to organize code in . Here are some reasons why enumeration comes in handy :

  • Use enumeration , You can create constants that are easy to associate , Make constants clearer
  • Developers are free to JavaScript Create memory efficient custom constants in . As we know ,JavaScript Enumeration is not supported , but TypeScript Can help us access them
  • TypeScript Use enumerated JavaScript Inline code in saves running time and compilation time
  • TypeScript Enumeration also provides that we used to have only in Java And other languages . This flexibility allows us to easily express and document our intentions and use cases

// The default from the 0 Start 
enum Direction{
    Up,
    Down,
    Left,
    Right,
}
console.log(Direction.Up)//0
console.log(Direction[0])//up

// After assignment, and so on 
enum Direction2{
    Up=1,
    Down,
    Left,
    Right,
}
console.log(Direction2.Down)//2
console.log(Direction2[2])//Down

// Assignment string 
enum Direction3{
    Up='UP',
    Down='DOWN',
    Left='LEFT',
    Right='RIGHT',
}
const value='UP'
if(value==Direction3.Up){
   console.log('go up');
}

原网站

版权声明
本文为[One at the front]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260612553522.html