当前位置:网站首页>ts中枚举类型(enum)简单使用
ts中枚举类型(enum)简单使用
2022-06-26 06:13:00 【前端一枚】
为什么要枚举?
枚举只是在 TypeScript 中组织代码的一种有用方式。以下是枚举派上用场的一些原因:
- 使用枚举,您可以创建易于关联的常量,使常量更加清晰
- 开发人员可以自由地在 JavaScript 中创建内存高效的自定义常量。正如我们所知,JavaScript 不支持枚举,但 TypeScript 可以帮助我们访问它们
- TypeScript 枚举使用 JavaScript 中的内联代码节省了运行时间和编译时间
- TypeScript 枚举还提供了我们以前只有在 Java 等语言中才有的一定灵活性。这种灵活性使我们可以轻松地表达和记录我们的意图和用例
//默认从0开始
enum Direction{
Up,
Down,
Left,
Right,
}
console.log(Direction.Up)//0
console.log(Direction[0])//up
//赋值后往后以此类推
enum Direction2{
Up=1,
Down,
Left,
Right,
}
console.log(Direction2.Down)//2
console.log(Direction2[2])//Down
//赋值字符串
enum Direction3{
Up='UP',
Down='DOWN',
Left='LEFT',
Right='RIGHT',
}
const value='UP'
if(value==Direction3.Up){
console.log('go up');
}边栏推荐
- Zotero使用之自定义参考文献格式
- Younger sister Juan takes you to learn JDBC -- two days' Sprint Day2
- SQL server functions
- 如何让主线程等待子线程执行完毕后再执行
- Message queuing - omnidirectional comparison
- 消息队列-功能、性能、运维对比
- 04. basic data type - list, tuple
- Web components series (10) -- realize the basic layout of mycard
- Redis multithreading and ACL
- Efk Upgrade to clickhouse log Storage Reality
猜你喜欢
随机推荐
Use the fast proxy to build your own proxy pool (mom doesn't have to worry about IP being blocked anymore)
Redis underlying data structure
自顶向下的变成方法
连接数服务器数据库报:错误号码2003Can‘t connect to MySQL server on ‘服务器地址‘(10061)
Level signal and differential signal
成水最多的容器
PyTorch混合精度原理及如何开启该方法
GoF23—抽象工厂模式
volatile应用场景
如何让主线程等待子线程执行完毕后再执行
Redis多线程与ACL
稀疏数组sparsearray
消息队列-消息事务管理对比
A tragedy triggered by "yyyy MM DD" and vigilance before New Year's Day~
Deeply uncover Ali (ant financial) technical interview process with preliminary preparation and learning direction
Understanding of nil in go language
numpy. random. choice
Keepalived to achieve high service availability
Redis底层数据结构
Summary of JVM interview focus (II) -- garbage collector (GC) and memory allocation strategy









