Default value argument for Map/WeakMap.prototype.get()

Tab Atkins Jr. jackalmage at gmail.com
Wed Nov 28 14:41:25 PST 2012


On Wed, Nov 28, 2012 at 1:16 PM, Andrea Giammarchi
<andrea.giammarchi at gmail.com> wrote:
> has is another story and your functions will fail.
> I have provided already this example which is perfectly valid:
>
> var m = new Map;
> m.set(m, undefined);
> m.has(m); // true

...what?  Why would my functions fail?  Assuming you use them instead
of the built-in map functions, they work just fine.

> all you need in JS, which is not Python, is the ||
>
> m.get(key) || defaultValue
>
> which is the most common case, as you wrote, for maps.

This fails if the value is 0, null, false, undefined, or any other falsey value.

> I strongly doubt you gonna store false, empty strings, or null as values,
> right?

It's completely reasonable to store those things, and I have done
exactly that when using dictionaries in languages like Python and
Lisp.

> It's also funny I have already implemented that and it is not me pushing
> back so ... I actually don't get anything you said or what you think I wrote

You said (paraphrasing, obviously) "there's no need to add this
feature, because authors can just add a one-line function that
accomplishes it".  I was just responding that this is a bad argument;
while the ease of coding it yourself is a relevant factor, it's not
the only one, as one should also consider how often it'll be used.

~TJ


More information about the es-discuss mailing list