3

Curious why the author surrounded object declaration with parenthesis here

rtpg.map.START_KEYS = ({"Key 1":"Value 1", "Key 2":"Value 2", "Key 3":"Value 3", "Key 4":"Value 4"});

why not:

rtpg.map.START_KEYS = {"Key 1":"Value 1", "Key 2":"Value 2", "Key 3":"Value 3", "Key 4":"Value 4"};
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
dev.e.loper
  • 35,446
  • 76
  • 161
  • 247

2 Answers2

3

There is no functional difference.

In the case that you emplace JSON text in an eval call to obtain a JavaScript object, you have to use parentheses to disambiguate the resulting expression from a statement, but that is not the case here: the object literal follows a = token and can therefore be parsed only as an expression, with no disambiguation required.

The author has therefore done it out of:

  • consistency, or
  • preference, or
  • ignorance.
Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

As others have mentioned, there is some value in specifying parenthesis when using eval, but that is not the case here. In this particular code snippet, either way would work exactly the same, so for whatever reason this was just the author's preference.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284