[Typescript] Typescript Enums vs Booleans when Handling State
2016-03-25 00:31
429 查看
Handling state with Typescript enums, instead of booleans, is preferred because:
- Enums are more readable
- Enums can have as many states as you need while booleans only have 2
- You only need to keep track of state with 1 variable when using enums
TypeScript enums are number based. This means that numbers can be assigned to an instance of the enum, and so can anything else that is compatible with
- Enums are more readable
- Enums can have as many states as you need while booleans only have 2
- You only need to keep track of state with 1 variable when using enums
TypeScript enums are number based. This means that numbers can be assigned to an instance of the enum, and so can anything else that is compatible with
number.
enum Color { Red, Green, Blue } var col = Color.Red; col = 0; // Effectively same as Color.Red
enum CardSuit { Clubs, Diamonds, Hearts, Spades } // Sample usage var card = CardSuit.Clubs; // Safety card = "not a member of card suit"; // Error : string is not assignable to type `CardSuit`
相关文章推荐
- 什么是C++虚函数、虚函数的作用和使用方法
- 单点登录SSO的实现原理
- 线上Linux服务器运维安全策略经验分享
- 深入Java虚拟机(1)——Java体系结构
- lua_c++
- 关于Markdown编辑器的详细使用
- Bzoj 1042: [HAOI2008]硬币购物 容斥原理,动态规划,背包dp
- 垃圾的clear函数------stringstream类型对象的清空操作之坑(不是用clear)
- HDU 1863 畅通工程
- 多线程学习
- Java学习-043-获取文件在目录中的路径
- Struts2中的类型转换器学习
- spring学习笔记(18)使用JNDI模拟访问应用服务器多数据源实例
- OSI与TCP/IP各层的结构与功能,都有哪些协议
- 数组形参
- Java命令行的执行参数
- Android中Fragment和Activity之间的通信(含接口回调)
- html细节上的错误
- 1-STM32学习笔记之系统架构
- Java关于父类引用指向子类对象