Use cases for WeakMap

Brendan Eich brendan at mozilla.com
Sun May 15 00:05:45 PDT 2011


On May 14, 2011, at 11:59 PM, Mark S. Miller wrote:

> On Sat, May 14, 2011 at 11:57 PM, Erik Corry <erik.corry at gmail.com> wrote:
> 
> On May 15, 2011 2:55 AM, "Boris Zbarsky" <bzbarsky at mit.edu> wrote:
> >
> > On 5/14/11 6:37 PM, Oliver Hunt wrote:
> >>
> >> Can you provide a use case where you have an object key as the usual programming idiom?
> >
> >
> > Attaching metadata to object without polluting the objects themselves.
> 
> That sounds more like private names
> 
> Private name and soft field lookup inherits. WeakMap lookup doesn't.

Also, depending on how they're spec'ed, private names are really naming properties "on" the object. Can't set them on frozen objects in the main (names, private names) proposals. This is a crucial difference with weak maps.

Besides attaching metadata, weak maps are important for remembering the wrapper or membrane for a given (frozen or not, built-in or "host", not to be mutated) object identity. Mark and Andreas knows too well, so I'm preaching to es-discuss in the To: line. This is not a use-case for weak references.

Weak maps are useful. I don't think they're misnamed. But we can revisit the name if we must.

/be

> 
>  
> >
> > For example, Firefox extensions want to do this all the time for various DOM objects (and especially Window).
> >
> > -Boris
> >
> > _______________________________________________
> > es-discuss mailing list
> > es-discuss at mozilla.org
> > https://mail.mozilla.org/listinfo/es-discuss
> 
> _______________________________________________
> 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

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


More information about the es-discuss mailing list