TypeScript小知识:遍历enum (暂时记录)

发布时间 2023-07-26 16:55:20作者: 赋流
enum BlockPrefab{
    BLOCK2 = 0,
    BLOCK4,
    BLOCK8,
    BLOCK16,
    BLOCK32,
    BLOCK64,
    BLOCK128,
    BLOCK256,
    BLOCK512,
    BLOCK1024,
    BLOCK2048
}

let num = BlockPrefab.BLOCK128;
let smth = BlockPrefab[num];
let xx = BlockPrefab[smth];

 ts 的enum编译为js如图所示

BlockPrefab["BLOCK2"] = 0 可以相互转化 以此来实现enum