wmock
wmock

Reputation: 5492

ES6 Symbols with Dot Notation

In the examples I've seen so far using Symbols in ES6, you have to access symbol properties in Object literals using bracket notation:

let mySymbol = Symbol("mySymbol");

let someObject = {
  [mySymbol]: "someValue"
};

console.log(someObject[mySymbol]); // "someValue"

Is there a way to define and access symbol properties using dot notation?

Upvotes: 2

Views: 965

Answers (2)

Bergi
Bergi

Reputation: 664433

No. Dot notation is reserved for identifiers which resolve to property names (strings). And this won't change for backward compatibility reasons.

Upvotes: 3

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

No. Symbols must be accessed using bracket notation.

Dot notation is only used for string keys that follow certain rule patterns, mostly about being a valid identifier.

Symbols are not strings, they are a whole something else entirely.

Short rational: One of the design goals of symbols is that they can not clash with property names, that makes them safe to use.

So, if you had an object like this

var = {
  prop1: "Value"
};

And you created a symbol called prop1, how could you tell the two apart, and access them differently, using just object notation?

Upvotes: 4

Related Questions