Last active
December 16, 2015 22:09
-
-
Save dmiro/5505098 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() { | |
var that = this; | |
for (var i in arguments) { | |
that[arguments[i]] = i; | |
} | |
this.name = function(value) { | |
for (var key in that) { | |
if (that[key] == value) { | |
return key; | |
} | |
} | |
}; | |
this.exist = function(value) { | |
return (typeof that.name(value) !== "undefined"); | |
}; | |
if (Object.freeze) { | |
Object.freeze(that); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
var Color = new Enum('RED', 'GREEN', 'BLUE');
undefined
Color.name(Color.REDs)
undefined
Color.name(Color.RED)
"RED"
Color.exist(Color.REDs)
false
Color.exist(Color.RED)
true