A different semantics for WeakMap#get default value

Tab Atkins Jr. jackalmage at gmail.com
Mon Jan 16 12:23:31 PST 2012

On Mon, Jan 16, 2012 at 12:09 PM, David Bruant <bruant.d at gmail.com> wrote:
> Thinking more about the current semantics, I thought that what is
> currently done could be achieved a bit differently:
> -----
> var v = wm.get(o, def);
> // almost equivalent to:
> var v = wm.get(o) || def;
> -----

This is a very common semantic in dictionaries, and it's extremely
useful.  I recommend adding it, either to get() as you describe, or in
a second function.  Python implements it both ways - the get()
function can take a second "default" value that is returned if the key
is not in the dict, and it provides a setdefault() function that sets
the key if and only if it's not already set, and then returns the
key's value.


More information about the es-discuss mailing list