In C# I might use an enumeration.
In JavaScript, how can I limit a value to a set of discrete values idiomatically?
We sometimes define a variable in a JS class 'Enumerations' along these lines:
var Sex = {
Male: 1,
Female: 2
};
And then reference it just like a C# enumeration.
There is no enumeration type in JavaScript. You could, however, wrap an object with a getter and setter method around it like
var value = (function() {
var val;
return {
'setVal': function( v ) {
if ( v in [ listOfEnums ] ) {
val = v;
} else {
throw 'value is not in enumeration';
}
},
'getVal': function() { return val; }
};
})();
value.setVal = 'debug Hell' you end up replacing the function with the string and that's where you are! ...Working now.const value = (function... I think.const wouldnt help you with that. You would need to use something like Object.freeze() if you want to account for this case.