Argument in favor of adding "has" in the WeakMap API

Brendan Eich brendan at mozilla.com
Wed May 11 12:54:43 PDT 2011


On May 11, 2011, at 12:44 PM, Oliver Hunt wrote:

> So you want to do
> if (map.has(bar))
>   wiffle = map.get(bar)
> 
> or some such?
> 
> The problem here is that you can't guarantee that GC won't happen between those two calls, and therefore you could still end up getting undefined in response to the get.

Not if bar refers to the key object, since it must be a strong ref.

If you do

  if (map.has(bar)) {
    bar = null;
    // might GC here, or do stuff that might
    wiffle = map.get(bar);
    . . .
  }

then you get what you deserve.


> GC is (from the language's perspective) non-deterministic, so any non-atomic operation with a weak map leads to "non-sensical" behaviour.

True, but this does not apply to

  if (map.has(bar))
    wiffle = map.get(bar);

as written.

/be


More information about the es-discuss mailing list