Skip to content

Instantly share code, notes, and snippets.

@danguilherme
Last active August 29, 2015 14:14
Show Gist options
  • Save danguilherme/007e6652075c7e441d1a to your computer and use it in GitHub Desktop.
Save danguilherme/007e6652075c7e441d1a to your computer and use it in GitHub Desktop.
JavaScript Enum
function Enum(items, startingValue) {
var count = startingValue || 0,
item;
for (var i = 0; i < items.length && (item = items[i]) ; i++) {
var splitted = item.split(':');
if (splitted.length == 2) {
count = Number(splitted[0]);
item = splitted[1];
}
this[this[item] = count++] = item;
}
Object.freeze(this);
}
// Usage example:
// var example = new Enum(["MINUS_ONE", "ZERO", "4:FOUR", "FIVE"], -1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment