A different semantics for WeakMap#get default value

David Bruant bruant.d at gmail.com
Mon Jan 16 14:11:58 PST 2012

Le 16/01/2012 22:46, Mark S. Miller a écrit :
> David and Axel, I don't get it. What's wrong with the get with the
> optional default value, as specified by that text and as currently
> implemented by FF?
This discussion started with me saying that "var v = wm.get(o,
someValue);" could be a shortcut for:
var v = wm.get(o);
if(v === undefined){
    v = someValue;
    order.set(o, v);
which is a pattern I'm writing over and over.
There is nothing inherently wrong with the current design, I just wanted
to see if the semantics of the default value could be added.

> What additional value does this contingent mutation add, regardless of
> how it's packaged?
What additional value adds the default value when something very close
is achieved with "wm.get(o) || defaultValue"?
Do you have use cases of storing falsy values in WeakMaps? If so, then
the difference matters.

> Do you really want "has" to answer true for a key merely because a
> previous get (or setdefault) with that key caused a default value to
> be stored?
If the get does a "set" only if the default value is different than
'undefined', I think it's worth it.

Taking a step back, what is the use case for:
wm.set(o, undefined);
wm.has(o); // true
It seems that it's more the use case of a WeakSet than a WeakMap, no?


More information about the es-discuss mailing list