var sym = Symbol();
is window[\'sym\']
which is already global scope.
But MDN says:
T
Global symbol registry exists across all iframes in a window. (As symbols can't be passed across workers, there's no observable concept of it being identical across workers, barring the existence of sidechannel inspections, eg by memory probing.)
Symbol.for
is not much different from implementing your own cache using an object reference. It's merely in-built and thus more convenient. Instead of Symbol.for('a')
, you can simply do:
obj['a']? obj['a'] : obj['a'] = Symbol()
.and maintain a ref to obj
.
In fact, since javascript does not provide an API to remove symbols in the global registry, its beneficial to do it the manual-caching way if you need to manually manage the memory of the registry.