I am trying to make it easier to create the contents of a javascript object so that the values depend on the names of the participants, something like this:
var obj = {
1: "you selected 1",
2: "wow, you selected 2",
3: "this time, you selected " + myName(),
137: "I think you've chosen " + myName() + " this time.",
513: myName() + " is the answer!"
};
Is it possible for a reference to a member name to be defined in the value definition using something like the intended function myName () ?
If no native method exists, then what is the recommended way to accomplish this?
You may ask, "Why does this guy need this weird way of generating objects?" , and the answer is this: in my code, the names of the participants / fields can change, the default values will be copied with the only difference being the link to the name of the participant, and I don’t want to define the numeric values in each key-value pair twice, so I’m going back call back the name inside the value definition.
source
share