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日)
背景: 今天启动了一个新的项目,由于要从之前的旧项目中拿过来一些代码,所以就看了下公司之前项目代码,发现有定义的常量类,也有枚举类,然后就在想着两者的功能差不多,那他们之间到底有什么区别呢,所以就决定 ...
随机推荐
- jmeter进行分布式压测过程与 注意事项
jmeter命令行运行但是是单节点下的, jmeter底层用java开发,耗内存.cpu,如果项目要求大并发去压测服务端的话,jmeter单节点难以完成大并发的请求,这时就需要对jmeter进行分布式 ...
- 通过 profiling 定位 golang 性能问题 - 内存篇 原创 张威虎 滴滴技术 2019-08-02
通过 profiling 定位 golang 性能问题 - 内存篇 原创 张威虎 滴滴技术 2019-08-02
- hessian-serialization
http://hessian.caucho.com/doc/hessian-serialization.html Table of Contents 1. Introduction2. Desig ...
- XCTF-phoenix100
前期工作 查壳无壳,界面是普通的输入flag点击验证 逆向分析 文件结构只有一个MainActively,查看MainActively代码 public class MainActivity exte ...
- scala之map,List,:: , +:, :+, :::, +++操作
scala之map,List操作 1.Map操作 2.List操作 2.1Demo1 2.2Demo2 3.:: , +:, :+, :::, +++ 1.Map操作 Map(映射)是一种可迭代的键值 ...
- 将字符串进行md5加密
import java.security.MessageDigest; public class MD5Tools { /** * 将字符串进行md5加密 */ public static Strin ...
- ubuntu14.04 ssh允许root用户远程登录
vi /etc/ssh/sshd_config #注释掉 #PermitRootLogin without-password # Authentication: LoginGraceTime 120 ...
- mysql高级day1
Mysql高级-day01 MySQL高级课程简介 序号 Day01 Day02 Day03 Day04 1 Linux系统安装MySQL 体系结构 应用优化 MySQL 常用工具 2 索引 存储引擎 ...
- Jenkins开启丢弃旧的构建?你可要小心啊!
玩Devops的小伙伴应该对Jenkins都有了解. Github上16.8k的Star的项目,1500+的构建.发布等自动化插件可供选择,事实上的业界CICD标准领导者. JFrog.Coding等 ...
- 简单判断long long 以内的回文数
bool Ispalindrome(long long x) { if(x<0||(x>0&&x%10==0))return false; long long back=0 ...