Skip to content

Instantly share code, notes, and snippets.

@dmiro
Last active December 16, 2015 22:09
Show Gist options
  • Save dmiro/5505098 to your computer and use it in GitHub Desktop.
Save dmiro/5505098 to your computer and use it in GitHub Desktop.
Javascript ENUM
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);
}
}
@dmiro
Copy link
Author

dmiro commented May 4, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment