WeakMap API questions?

Erik Arvidsson erik.arvidsson at gmail.com
Sat Aug 14 15:16:39 PDT 2010


I think Ash brings up a valid point here. From a usability point it is
a clear win to use [], delete and in for WeakMaps and Maps.

On Sat, Aug 14, 2010 at 14:02, Mark S. Miller <erights at google.com> wrote:
> On Sat, Aug 14, 2010 at 1:01 PM, Ash Berlin <ash_js at firemirror.com> wrote:
>>
>> On 14 Aug 2010, at 07:22, Erik Arvidsson wrote:
>> > I have a few questions regarding the WeakMap API.
>> >
>> > 1. Why isn't there a way to check for presence of a key (using
>> > has/contains)?
>> >
>> > Given that undefined is a valid value it is not sufficient to just
>> > return undefined for get
>>
>> Does the standard trick of:
>>
>>  if (key in weakMapInstance) { }
>>
>> not work?
>
> It does not. A key is not a property name. A weak map is an object with two
> own properties, names "get" and "set", whose values are the methods that
> constitute the weak map API.
>
>>
>> >
>> > 2. Why isn't there a way to remove a key-value-pair?
>> >
>> > Setting the value to undefined is not the same.
>>
>> Again:
>>
>>  delete weakMapInstance[key];
>
> No. This syntax deletes named properties.
>
>>
>> > 3. Why isn't there a way to iterate over the keys?
>> >
>> > I can see that this might be a security issue but iteration is useful
>> > and security sensitive code can prevent iteration in several ways.
>>
>>  Object.keys(weakMapInstance)
>
> No. Object.keys enumerates property names.
>
>>
>> >
>> > 4. Why does set throw if the key is not an object but get doesn't?
>> >
>> > Same would go for delete and has if those are added.
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> --
>     Cheers,
>     --MarkM
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>



-- 
erik


More information about the es-discuss mailing list