WeakMap API questions?

Ash Berlin ash_js at firemirror.com
Sat Aug 14 17:04:53 PDT 2010


On 14 Aug 2010, at 23:58, Mark S. Miller wrote:
> On Sat, Aug 14, 2010 at 3:16 PM, Erik Arvidsson <erik.arvidsson at gmail.com> wrote:
> 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.
> 
> We've been over this territory before. How would you reconcile this (quite sensible) desire with the existing semantics of [], delete, and in, and with the existing code that depends on this semantics?
> 
> Another pain point is implementation cost on uninvolved code. I had earlier proposed a change to Proxy semantics to achieve exactly this. (Because, again, I do find the idea desirable.) It failed because it would have required postponing the coercion of an index from object to string until one knew the type of object one was indexing into. IIRC, this was not free for uninvolved code on some implementations. I'm rather confident that any new feature that slows down code not using that feature will fail to achieve consensus.
> 
> If a painless reconciliation can be found, I'd probably be supportive. Suggestions welcome.

Okay I can see (from an implementation PoV if not usability) why |[]|, |delete| and |in| can't work, but is there anything that says the Object.keys has to return strings and can't just return an array of Objects for a WeakMap? Not sure if this makes the Object MOP layer cleaner or not...

-ash

> 
> 
> 
> 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.
> >>
> >>
> -- 
>     Cheers,
>     --MarkM


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20100815/e7cae415/attachment.html>


More information about the es-discuss mailing list