A different semantics for WeakMap#get default value

Mark S. Miller erights at google.com
Mon Jan 16 14:23:01 PST 2012


On Mon, Jan 16, 2012 at 2:11 PM, David Bruant <bruant.d at gmail.com> wrote:
[...]

> > 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.


That's precisely what I'm confused about. I agree that "||" doesn't work
when the map is used to store falsy values. But the FF get/2 works
perfectly fine:


> !!0;
false
> !!'';
false
> var wm = WeakMap();
> var x = {};
> var y = {};
> wm.set(x, 0)
> wm.get(x, '')
0
> wm.get(y, '') === '';
true
> wm.has(x)
true
> wm.has(y)
false


That seems ideal.

-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120116/d26a6dd6/attachment-0001.html>


More information about the es-discuss mailing list