Javascript Object enumeration syntax -
this question has answer here:
i have object:
var state = { waiting: 1, ready: 2 }
and object getting texts state:
var language = { states: { waiting: "waiting..", ready: "ready!" } }
i want text directly state this:
var state = state.ready; var statustext = language.states[state];
for work need, like, illegal syntax:
var language = { states: { state.waiting: "waiting..", state.ready: "ready!" } }
but doesn't work, can use text state variable?
no, don't. make values sensible instead of magic numbers. in case, make them use strings map key names other object(s) use, , 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 state numbers mean something, make lookup them, too:
var responsecodes = { "0": "waiting", "1": "reading", "403": "forbidden", ... }
and chain lookups:
... var responsecode = whatever(); var state = responsecodes[responsecode]; var label = language.state[state]; ...
Comments
Post a Comment