A ghost symbol inside the object void

Symbols are completely unique

This simple principle can cause some interesting consequence, like a sad ghost symbol, sitting in the dark out of our reach, shouting into the void.

Let’s say we have an object with a symbol…

let o = {
   [Symbol('symbol')]:'Hi, im here!!!'
}

If we will look at it on the console, we see it’s there:

{Symbol(symbol): "Hi, im here!!!"}

But if we try to call it…

o[Symbol('symbol')] == undefined;

By definition, if we try to enumerate for it, we will see that our object has no keys.

for(let key of Object.keys(o)){
  console.log(keys)
}

The cause of all this is simple: Symbols are completely unique

Symbol("symbol") != Symbol("symbol")

The correct way to use symbol is:

ley symbol =Symbol('symbol');
let o = {
   [symbol]:'Hi, im here, and you can read me!!!'
}
o[symbol] =='Hi, im here, and you can read me!!!';

Just before I wrote a eulogy on the unreachable symbol, I found a way to reincarnate it, and bring it back out of the void.

o[Object.getOwnPropertySymbols(o)[0]]

Or nicer written:

let symbol = Object.getOwnPropertySymbols(o)[0];
o[symbol] =="Hi, im here!!!";

We still need to find a way to handle more than one symbol, but our own symbolic ghost had come alive. It’s alive, It’s alive!!!