That is not how Symbol.for
works.
If you create a new Symbol
using the Symbol
function, you will get a unique symbol every time. Example:
const sym1 = Symbol("x");
const sym2 = Symbol("x");
sym1 === sym2; // returns false
If you want to use a global symbol, you will have to define it using Symbol.for
as well:
const sym = Symbol.for("x");
let obj = {
[sym]: "val"
}
console.log(obj[Symbol.for("x")]); // "val"
console.log(obj[sym]); // "val"
const sym = Symbol("x");
let obj = {
[sym]: "val"
}
console.log(obj[Symbol("x")]); // undefined
console.log(obj[sym]); // "val"