0

I have this object:

var State = {
  WAITING: 1,
  READY: 2
}

and this object for getting texts for the state:

var Language = {
  States: {
    WAITING: "Waiting..",
    READY: "Ready!"
  }
}

I want to get the text directly from state like this:

var state = State.READY;

var statusText = Language.States[state];

for this to work I need, something like, this illegal syntax:

var Language = {
  States: {
    State.WAITING: "Waiting..",
    State.READY: "Ready!"
  }
}

but this doesn't work, what can I use to simply get the text from state variable?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
eguneys
  • 6,028
  • 7
  • 31
  • 63
  • 1
    There are existing questions about dynamic keys in object literals, many of which have noted the relevant change in ES6. Example: [Using a variable for a key in a JavaScript object literal](http://stackoverflow.com/questions/2274242/using-a-variable-for-a-key-in-a-javascript-object-literal) – Jonathan Lonowski Jun 01 '15 at 04:47
  • [MDN - Object initializer - Computed property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names) – Jonathan Lonowski Jun 01 '15 at 04:51
  • Ironic that your name is "facebook"; you might be interested in [keymirror](https://www.npmjs.com/package/keymirror). It was developed by facebook. – Mulan Jun 01 '15 at 04:51
  • and marked as duplicate by a facebook employee lol – Mulan Jun 01 '15 at 04:53
  • @FelixKling sure this is a duplicate in the narrow sense of "how to I specify a variable object key", but actually it's not in the broad sense of "how to I implement enums in JS". People coming from C etc. are use dto enums as a set of "names" that usually have integer values. The OP's issue is not actually that he wants syntax for variable property names, it's that he is thinking of enums like they exist In C, which made him think we wanted to be able to say `States: { State.WAITING: `. So I don't think it's a duplicate in that sense and should be reopened for good answers. –  Jun 01 '15 at 12:05

3 Answers3

1

No, you don't. Just make your values be sensible instead of magic numbers. In this case, make them use strings that map to the key names that your other object(s) use, and done:

var States = {
  WAITING: "WAITING",
  READY: "READY"
}

var Language = {
  States: {
    WAITING: "Waiting..",
    READY: "Ready!"
  }
}

Voila:

var state = States.WAITING;
var label = Language.States[state]

And if those state numbers mean something, then make a lookup for them, too:

var responseCodes = {
  "0": "WAITING",
  "1": "READING",
  "403": "FORBIDDEN",
  ...
}

And then just chain your lookups:

...
var responseCode = whatever();
var state = responseCodes[responseCode];
var label = Language.State[state];
...
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • 1
    But why have a hash whose only job is to map strings to themselves? Instead of saying `state = States.WAITING`, it would be equivalent to just say `state = 'WAITING'`. –  Jun 01 '15 at 04:50
  • obviously? but the original question creates an extracted state, so it's trivial to bypass that, but let's keep it in the answer so that if that's actually necessary, there it is. – Mike 'Pomax' Kamermans Jun 01 '15 at 15:54
1

You may be coming from a language background where enums are a collection of names that represent integer values. It's easier in JS to use strings for enum values, and then you can use them directly as keys to look up things in hashes, or use them directly in a switch statement, so you don't need the State variable at all. All you need is

var Language = {
  States: {
    WAITING: "Waiting..",
    READY: "Ready!"
  }
}

var state = 'READY';

var statusText = Language.States[state];
1

Try this:

var State = {
  WAITING: 1,
  READY: 2
};

var Language = {
  States: {}
};

Language.States[State.WAITING] = "Waiting..";
//evaluated as, Language.States[1] = "Waiting..";
Language.States[State.READY] = "Ready!";

var state = State.READY;

var statusText = Language.States[state];

alert(statusText);