Enum

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

目前為 2019-11-03 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/391854/746332/Enum.js

作者
Gerrit
版本
0.2
建立日期
2019-11-01
更新日期
2019-11-03
尺寸
2.5 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"