> 2. For security purposes, only code that has been explicitly given access to _both_ the weak map and a key may gain access to the value stored within the > weakmap. This is why there is no enumeration—if there were, then any code that could access the weak map could then also access all of the keys and > > values. If that’s a desirable trait, then just use a Map.
What is the problem in accessing the keys with any code, which is the same behaviour for any other object in javascript: String, Array, Object and so on. The point is being able to do on a weak key so code can be processed without being retained by the GC. On 4 November 2015 at 22:36, Rick Waldron <[email protected]> wrote: > > > On Wed, Nov 4, 2015 at 8:16 AM Coroutines <[email protected]> wrote: > >> On Wed, Nov 4, 2015 at 4:56 AM, Romuald Quantin <[email protected]> >> wrote: >> >>> As an aside and as Coroutines, >>> >>> I never understood why there is this inability to enumerate WeakMap keys. >>> >> >> If I had it my way there would be no WeakMap or WeakSet. I'd have a >> Symbol.mode similar to Lua's __mode meta(method/field?) that would show the >> garbage collector that keys or values are weakly referenced in an object. >> Or even: >> >> obj[Symbol.weakValues] = true; >> obj[Symbol.weakProperties] = true; >> >> WeakMap isn't really all that special. Set is important for how it >> maintains only unique values are inserted - but you could do this with >> Proxy. >> > > This is all very misunderstood (both Romuald and Coroutines comments). > WeakMap is extremely special, for these reasons: > > 1. The weakly mapped relationship between the key and value allows the > runtime to gc the value once the key becomes otherwise unreachable. > Specifically: if the _only_ reachable reference to the key is the weakmap > relationship, then that relationship can be discarded and the value can be > garbage collected at some implementation-determined time in the future. > 2. For security purposes, only code that has been explicitly given access > to _both_ the weakmap and a key may gain access to the value stored within > the weakmap. This is why there is no enumeration—if there were, then any > code that could access the weakmap could then also access all of the keys > and values. If that's a desirable trait, then just use a Map. > > These things cannot be done with Proxy, because anything that Proxy does > will inevitably be a strongly held reference. > > > Rick > -- www.soundstep.com
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

