Skip to content

Instantly share code, notes, and snippets.

@danguilherme danguilherme/Enum.js
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.