Last active
August 29, 2015 14:14
-
-
Save danguilherme/007e6652075c7e441d1a to your computer and use it in GitHub Desktop.
JavaScript Enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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