I'm a bit confused about the role of Symbols if they can't be easily accessed. I understand that you can create one using a key with for
and then retrieve it with keyFor
.
let keySymbol = Symbol.for("XXX");
console.log(Symbol.keyFor(keySymbol) // "XXX";
But when looking at the example below, why would someone do this:
let obj = {
name: "Mike",
age: 44,
}
obj[Symbol()] = false;
The symbol field doesn't show up when calling console.log(obj)
, nor when iterating through the object. So, what is the purpose of adding a symbol if it cannot be accessed or utilized in any way?