Enum.js
Simple Enumerable object in javascript.
Usage
Creating custom Enum
Enum.extend function expect two parameters:
- first is a static properties aka Enum constants,
- second is a custom prototype methods.
var HTTPCodeEnum = Enum;
You can also pass in first object:
- static functions, that will can be accessed like
MyEnum.myFunction()
- static objects,
- static values, needed for internal usage. To prevent parsing them as Enum constant, add double underscore as prefix, like:
__myInternalValue
Enum throws error in some usual cases. So there is an ability to use custom Errors for your custom Enums, to catch them upper and check with instanceOf
function:
var HTTPCodeEnum = Enum;
If
__error
does not present in first parameter object, then Enum will use its own EnumError constructor. Anyway, u can always get the reference to the Error function viaEnum.__error
Simple usage
{ if datacode == HTTPCodeEnumOK // ... }
Enum usage:
{ // Throws a EnumError if catch non existing code var status = datacode; // Returns mapped message // getMessage must be implemented in HTTPCodeEnum.prototype console;} // Exceptions handling { try var request = $; catche if e instanceof HTTPCodeEnum__error console; }