WeakMap API questions?

Erik Corry erik.corry at gmail.com
Thu Sep 2 00:46:13 PDT 2010


2010/8/14 Mark S. Miller <erights at google.com>:
> 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.

And this is as it should be.  As it stands the weak map can be used as
an object with private members.  The object key acts as a capability
that controls whether or not you have access to the private member.
If you are allowed to enumerate the keys then privacy goes out of the
window.

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


More information about the es-discuss mailing list