I want to use a so called "enum" object as dictionary key,(actually,it is also a dictionary AFAIK), in JavaScript.
This doesn't work:
    var State ={DEFAULT:0,ACTIVE:1 ,INACTIVE:2,ALERT:3};
    var statesDict =  {
      State.ACTIVE : {color:0x00ff00}
      State.INACTIVE: {color:0x000000}
    };
while this one does:
   var State ={DEFAULT:0,ACTIVE:1 ,INACTIVE:2,ALERT:3};
    var statesDict =  {
      1: {color:0x00ff00}
      2: {color:0x000000}
    };
Why? Isn't the State.{prop name} supposed to be replaced by its value? 
You could use computed property names with brackets.
var State = { DEFAULT: 0, ACTIVE: 1, INACTIVE: 2, ALERT: 3 },
    statesDict = {
        [State.ACTIVE]: { color: 0x00ff00 },
        [State.INACTIVE]: { color: 0x000000 }
    };
console.log(statesDict);
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