|
OEnum
Overview of OEnum
OEnumCreates a new enum class along with the specified instances. Usagevar MyEnum = OEnum( values ); or var MyEnum = OEnum( object );
MethodsThe created instances have some helpful methods to interrogate the enum. getValueGets the value of the item. getNameGets the name of the item. ExampleThe following code creates an enum class called Sizes: var Sizes = OEnum("Small", "Medium", "Large");A new class will be created called Sizes and three static instances will be generated, namely: Sizes.Small Sizes.Medium Sizes.Large Specifying custom valuesTo provide custom values (instead of just an increasing index counter) you can pass a single object to OEnum: var Sizes = OEnum({
Small: 100,
Medium: 200,
Large: 300
});
Sizes.fromValue(100);
// returns Sizes.Small
Sizes.fromValue(200);
// returns Sizes.Medium
Sizes.fromValue(300);
// returns Sizes.LargeChecking the type of an enum valueEach enum value is the type (.kind) of the enum itself. For example, Sizes.Small is of type Sizes, so the following code returns true: alert( Sizes.Small.kind === Sizes ); Reflecting on an enumThe enum class that gets generated contains a static array called names which allows you to iterate over the acceptable items. var Sizes = OEnum("Small", "Medium", "Large");
Sizes.names
// returns ["Small", "Medium", "Large"]The value associated with the name is the position at which it occurs in the array, so Small is 0, Medium is 1 and Large is 2. Looking up an instance by valueTo look up an enum instance by the value, use the static fromValue method: var Sizes = OEnum("Small", "Medium", "Large");
Sizes.fromValue(0);
// returns Sizes.Small
Sizes.fromValue(1);
// returns Sizes.Medium
Sizes.fromValue(2);
// returns Sizes.Large
| ||||