0

When creating an object containing a key value pair, binding that to a variable and then accessing that property, it works fine:

var b = { foo: 'value' };
b.foo;
// "value"

If I create a similar object, directly access its property and bind that to a variable, it works as well:

var b = { foo: 'value' }.foo;
b;
// "value"

However, when submitting the following code to the console, this results in an error:

{ foo: 'value' }.foo;
// Uncaught SyntaxError: Unexpected token '.'

Why?

Wasbeer
  • 389
  • 2
  • 11
  • 5
    Because `{` at the beginning of the line (the beginning of a statement really) denotes the beginning of a block, not an object literal. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block – Felix Kling Feb 20 '20 at 13:09
  • 1
    Also, `({ foo: 'value' }).foo` will work fine. – palaѕн Feb 20 '20 at 13:16

0 Answers0