TypeScript enum 枚举实现原理
TypeScript enum 枚举实现原理
反向映射
https://www.typescriptlang.org/docs/handbook/enums.html
enum Direction {
Up,
Down,
Left,
Right
}
TypeScript enum 枚举实现原理,反向映射
"use strict";
var Direction;
(function (Direction) {
Direction[Direction["Up"] = 1] = "Up";
Direction[Direction["Down"] = 2] = "Down";
Direction[Direction["Left"] = 3] = "Left";
Direction[Direction["Right"] = 4] = "Right";
})(Direction || (Direction = {}));
const log = console.log;
log(`Direction =`, Direction)
/*
{
1: "Up", 2: "Down", 3: "Left", 4: "Right",
Up: 1, Down: 2, Left: 3, Right: 4,
}
*/

"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-21
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
function logString(msg: string): void {
log(`message =`, msg)
}
function logNumber(msg: number): void {
log(`message =`, msg)
}
// number enum
enum DirectionNumber {
Up,
Down,
Left,
Right
}
// number enum with change index
enum DirectionIndex {
Up = 3,
Down,
Left,
Right
}
// string & number mixed enum
enum DirectionMixed {
Up = "up",
Down = 1,
Left,
Right
}
// string enum
enum Level {
A = "perfect",
B = "good",
C = "bad",
}
// const enum
const enum Roles {
Admin,
User,
Operator,
}
//number enum
enum Direction {
Up = 1,
Down,
Left,
Right
}
// TypeScript enum 枚举实现原理,反向映射
// Direction ={
// 1: "Up", 2: "Down", 3: "Left", 4: "Right",
// Up: 1, Down: 2, Left: 3, Right: 4,
// }
logString(Level.A);
logNumber(Direction.Down);
logNumber(Roles.Admin);
// console.log(0 /* Admin */);
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-21
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
var log = console.log;
function logString(msg) {
log("message =", msg);
}
function logNumber(msg) {
log("message =", msg);
}
// number enum
var DirectionNumber;
(function (DirectionNumber) {
DirectionNumber[DirectionNumber["Up"] = 0] = "Up";
DirectionNumber[DirectionNumber["Down"] = 1] = "Down";
DirectionNumber[DirectionNumber["Left"] = 2] = "Left";
DirectionNumber[DirectionNumber["Right"] = 3] = "Right";
})(DirectionNumber || (DirectionNumber = {}));
// number enum with change index
var DirectionIndex;
(function (DirectionIndex) {
DirectionIndex[DirectionIndex["Up"] = 3] = "Up";
DirectionIndex[DirectionIndex["Down"] = 4] = "Down";
DirectionIndex[DirectionIndex["Left"] = 5] = "Left";
DirectionIndex[DirectionIndex["Right"] = 6] = "Right";
})(DirectionIndex || (DirectionIndex = {}));
// string & number mixed enum
var DirectionMixed;
(function (DirectionMixed) {
DirectionMixed["Up"] = "up";
DirectionMixed[DirectionMixed["Down"] = 1] = "Down";
DirectionMixed[DirectionMixed["Left"] = 2] = "Left";
DirectionMixed[DirectionMixed["Right"] = 3] = "Right";
})(DirectionMixed || (DirectionMixed = {}));
// string enum
var Level;
(function (Level) {
Level["A"] = "perfect";
Level["B"] = "good";
Level["C"] = "bad";
})(Level || (Level = {}));
//number enum
var Direction;
(function (Direction) {
Direction[Direction["Up"] = 1] = "Up";
Direction[Direction["Down"] = 2] = "Down";
Direction[Direction["Left"] = 3] = "Left";
Direction[Direction["Right"] = 4] = "Right";
})(Direction || (Direction = {}));
// TypeScript enum 枚举实现原理,反向映射
// Direction ={
// 1: "Up", 2: "Down", 3: "Left", 4: "Right",
// Up: 1, Down: 2, Left: 3, Right: 4,
// }
logString(Level.A);
logNumber(Direction.Down);
logNumber(0 /* Admin */);
tsconfig bug ???
const timestamp = Date.now();
// const timestamp = new Date().getTime();
// computed enum
const enum Dynamic {
role = Roles.User,
level = Level.A,
// time = Date.now(),
// time = new Date().getTime(),
time = timestamp,
// time = Math.random(),
value = 1 + 2,
len = "123".length,
}
// const enum member initializers can only contain literal values and other computed enum values.

OK

computed enum const bug
remove
constkeyword
// computed enum const bug
// const enum DynamicConstBug {
// role = Roles.User,
// level = Level.A,
// time = Date.now(),
// timestamp = new Date().getTime(),
// random = Math.random(),
// value = 1 + 2,
// len = "123".length,
// }
// const enum member initializers can only contain literal values and other computed enum values.
// computed enum
enum Dynamic {
role = Roles.User,
level = Level.A,
time = Date.now(),
timestamp = new Date().getTime(),
random = Math.random(),
value = 1 + 2,
len = "123".length,
}
refs
https://www.typescriptlang.org/docs/
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
TypeScript enum 枚举实现原理的更多相关文章
- Enum枚举法
java enum(枚举)使用详解 + 总结 enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性,存放在 java.lang 包中. 下面是我在使用 enum 过程 ...
- Enum 枚举类
目录 Enum 枚举类 基础 定义与用途 基本方法 示例 进阶 实现原理 枚举与Class对象 自定义枚举类和构造方法及toString() Enum中使用抽象方法来实现枚举实例的多态性 Enum与接 ...
- TypeScript入门七:TypeScript的枚举
关于枚举 数字枚举 字符串枚举 异构枚举 计算的和常量成员 运行时的枚举与反向映射 常量枚举与外部枚举 一.关于枚举 枚举:一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计 ...
- c# (ENUM)枚举组合类型的谷歌序列化Protobuf
c# (ENUM)枚举组合类型的谷歌序列化Protobuf,必须在序列化/反序列化时加上下面: RuntimeTypeModel.Default[typeof(Alarm)].EnumPassthru ...
- C#将Enum枚举映射到文本字符串
介绍 当将以前的C代码移植到C#中时,我快发疯了,因为有很多的数组需要将常量映射到字符串.当我在寻找一个C#的方法来完成的时候,我发现了一个自定义属性和映射的方法. 如何使用代码? 对每一个enum枚 ...
- MVC3不能正确识别JSON中的Enum枚举值
一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...
- 161208、Java enum 枚举还可以这么用
在大部分编程语言中,枚举类型都会是一种常用而又必不可少的数据类型,Java中当然也不会例外.然而,Java中的Enum枚举类型却有着许多你意想不到的用法,下面让我们一起来看看. 先来看一段代码示例: ...
- Python中模拟enum枚举类型的5种方法分享
这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下 以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...
- java之enum枚举(2015年05月28日)
背景: 今天启动了一个新的项目,由于要从之前的旧项目中拿过来一些代码,所以就看了下公司之前项目代码,发现有定义的常量类,也有枚举类,然后就在想着两者的功能差不多,那他们之间到底有什么区别呢,所以就决定 ...
随机推荐
- 如何将python中pip源设置为国内源
1.Windows Python的学习过程中,往往会学习到很多库,而安装各类库的时候,往往不尽人意,下载速度从几KB到十几KB.甚至下载到一半还超时报错.这都是因为pip源是访问国外的官方源,如果需要 ...
- 有状态(Stateful)应用的容器化 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1020178
有状态(Stateful)应用的容器化 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1020178
- HTTPS学习(一):准备知识
div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...
- BIO,NIO,AIO 总结
BIO,NIO,AIO 总结 Java 中的 BIO.NIO和 AIO 理解为是 Java 语言对操作系统的各种 IO 模型的封装.程序员在使用这些 API 的时候,不需要关心操作系统层面的知识,也不 ...
- Trie(字典树)
没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...
- CF1416D 做题心得
CF1416D 做题心得 上次在某trick中提到了这个题,一开始觉得太毒瘤没有写,现在把它补上了. 感觉实现这个东西,比单纯收获一个trick,收获的东西多太多了. 主要思路 它的主要trick是& ...
- (15)Linux命令基本格式
1.命令提示符 登录系统后,第一眼看到的内容是: [root@localhost ~]# 这就是 Linux 系统的命令提示符.那么,这个提示符的含义是什么呢? []:这是提示符的分隔符号,没有特殊含 ...
- 学习一下 SpringCloud (四)-- 服务降级、熔断 Hystrix、Sentinel
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- 从官方文档中探索MySQL分页的几种方式及分页优化
概览 相比于Oracle,SQL Server 等数据库,MySQL分页的方式简单得多了,官方自带了分页语法 limit 语句: select * from test_t LIMIT {[offset ...
- 纯js添加类
1.el.setAttribute('class','abc'); <!DOCTYPE HTML><HTML><HEAD><meta charset=&quo ...