Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surround an object declaration with parenthesis [closed]

Tags:

javascript

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"};
like image 800
dev.e.loper Avatar asked Feb 07 '26 17:02

dev.e.loper


1 Answers

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.
like image 54
Lightness Races in Orbit Avatar answered Feb 09 '26 12:02

Lightness Races in Orbit