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; }
   };
 })();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With