Created
April 6, 2015 21:15
-
-
Save bendem/7224bfdfa31d25613abd to your computer and use it in GitHub Desktop.
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
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