What is 'global symbol registry'?

后端 未结 3 1960
春和景丽
春和景丽 2020-12-09 10:59
var sym = Symbol();

is window[\'sym\'] which is already global scope.

But MDN says:

T

3条回答
  •  独厮守ぢ
    2020-12-09 11:55

    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.

提交回复
热议问题