1

I would like to use ECMAscripts "bracket notation" to use the key of an object as a name (which will be displayed in my menu, thus it can not be a short name without space like in propertyName).

In a word, I would like to write

object["Property name"]

instead of

object.propertyName

Is there any reason why I should not do that (except that the "bracket notation" is a bit longer)?

arthur.sw
  • 11,052
  • 9
  • 47
  • 104
  • 1
    [Square bracket seems to slower (in microscopic terms)](http://jsperf.com/square-bracket-vs-dot), but other than that; there is no difference I can think of. – Matt Jul 05 '14 at 13:32
  • ECMAscript doesn't automatically convert between `["Property name"]` and `.PropertyName`. These are _different_ properties, and you cannot refer to `"Property name"` with dot notation. – Michael Berkowski Jul 05 '14 at 13:33

1 Answers1

0

As you have stated, you can't use dot notation (obj.property), when there is space in variable name. So if your variable name does contain space character, than use ["bracket notation"]

Justinas
  • 41,402
  • 5
  • 66
  • 96