WeakMap API questions?

Mark S. Miller erights at google.com
Sat Aug 14 14:02:10 PDT 2010


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20100814/ef2670c5/attachment.html>


More information about the es-discuss mailing list