Here is a brief test on using JavaScript Symbols as object keys for an object to achieve information hiding and stay compatible with other code that uses that object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Welcome to Node.js v14.15.4.
Type ".help" for more information.
> let a = Symbol("a")
undefined
> b = {}
{}
> b.a = a
Symbol(a)
> b
{ a: Symbol(a) }
> JSON.stringify(b)
'{}'
> Object.getOwnPropertyNames(b)
[ 'a' ]
> Object.getOwnPropertySymbols(b)
[]
> b[Symbol("c")] = "c"
'c'
> b
{ a: Symbol(a), [Symbol(c)]: 'c' }
> Object.getOwnPropertySymbols(b)
[ Symbol(c) ]
> Object.getOwnPropertyNames(b)
[ 'a' ]
> JSON.stringify(b)
'{}'
> b.hasOwnProperty("a")
true
> b.hasOwnProperty(Symbol("c"))
false
> b[Symbol("c")]
undefined
> b[Symbol(c)]
> b[Object.getOwnPropertySymbols(b)[0]]
'c'
> b[Object.getOwnPropertySymbols(b)[0]]
'c'

In a daily senario, Object.keys() and Object.getOwnPropertyNames() won’t access the Symbol keys in an object. getOwnPropertySymbols() is used to reveal those keys.

It is common to register Symbols in a global registry by using Symbol.for(). In that way, the symbol created with a given string name can be easily retrieved. Otherwise, techniques such as finding through all the symbols with Object.getOwnPropertySymbols() and Reflect may be used.

For further exploration, please navigate to the offical MDN documentaton.