Take the 2-minute tour ×

I know I can do this in JavaScript:

RequestDate: {
    '0' : 'previous_activity_date',
    '1': 'next_activity_date'

And I can do this:

this.RequestDate = {};
this.RequestDate[App.FORWARD] = 'next_activity_date';
this.RequestDate[App.BACK] = 'previous_activity_date';

Is there a way of making the following work:

RequestDate: {
    App.Back : 'previous_activity_date',
    App.Forward: 'next_activity_date'

The above obviously errors, is there a way to make it work?

share|improve this question
add comment

2 Answers

As you have noticed, you can write this:

RequestDate = {};
RequestDate[App.Back] = 'previous_activity_date';
RequestDate[App.Forward] = 'next_activity_date';

But the javascript syntax does not allow an expression before the : of the JSON notation.

share|improve this answer
How is that different to the OP? –  RobG Jun 14 '12 at 9:01
Well that was different by the time i Wrote the anwser. I guess that OP improved the question... ;=) –  Samuel Rossille Jun 14 '12 at 9:15
The moving finger, having writ… what… you CAN change it? Damn!! –  RobG Jun 14 '12 at 23:58
add comment

Short answer: no. You can only have literal keys in the object literal. Your own solution is the best one if you want to use constants.


11.1.5 Object Initialiser

An object initialiser is an expression describing the initialisation of an Object, written in a form resembling a literal. It is a list of zero or more pairs of property names and associated values, enclosed in curly braces. The values need not be literals; they are evaluated each time the object initialiser is evaluated.

share|improve this answer
add comment

Your Answer


