Enum

Enumeration class. Each enum propertiy has the properties "ordinal", "name" and "text".

目前为 2019-11-03 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/391854/746197/Enum.js

作者
Gerrit
版本
0.2
创建于
2019-11-01
更新于
2019-11-03
大小
2.4 KB
许可证
暂无

Usage Examples


class COLOR extends Enum {};
COLOR.init([ { "RED": "red" }, { "GREEN": "green" }, { "BLUE": "blue" } ]);
let col = COLOR.GREEN;

console.log(COLOR.GREEN.name);       // "GREEN"
console.log(COLOR.GREEN.text);       // "green"
console.assert(COLOR.GREEN.ordinal); // "1"

console.log(col[1] === COLOR.GREEN); // "true"

console.assert(col + ""); // "green"
console.assert(col * 1);  // "1"



class COLOR extends Enum {};
COLOR.init([ "RED", "GREEN", "BLUE" ]);

console.log(COLOR[2].name)            // "BLUE"
console.log(COLOR[2].text);           // ""
console.log(COLOR[2].ordinal);        // "2"

console.log(COLOR[2] === COLOR.BLUE); // "true"

console.log(col + ""); // "BLUE"
console.log(col * 1)   // "2"



class FLAGS extends Enum {};
FLAGS.init([ "FIRST", "SECOND", "THIRD", "FOURTH"], 1, ord => ord<<1);

console.log(FLAGS.FOURTH | FLAGS.SECOND); // "10"