Skip to content

Instantly share code, notes, and snippets.

@bendem
Created April 6, 2015 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendem/7224bfdfa31d25613abd to your computer and use it in GitHub Desktop.
Save bendem/7224bfdfa31d25613abd to your computer and use it in GitHub Desktop.
var Enum = function(values, methods) {
var created = {};
var EnumValue = function(value) {
this.value = value;
}
if(methods) {
for(var name in methods) {
EnumValue.prototype[name] = methods[name];
}
}
var array = values instanceof Array;
for(var i in values) {
if(array) {
created[values[i]] = new EnumValue(i);
} else {
created[i] = new EnumValue(values[i]);
}
}
return Object.freeze(created);
}
// ----------------------------------
// Example
// ----------------------------------
// var Direction = Enum({
// Right: 0,
// Up: 1,
// Left: 2,
// Down: 3,
// }, {
// isVertical: function() {
// switch(this) {
// case Direction.Right:
// case Direction.Left:
// return true;
// default:
// return false;
// }
// }
// , isHorizontal: function() {
// return !this.isVertical();
// }
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment