Use cases for WeakMap

Boris Zbarsky bzbarsky at MIT.EDU
Sun May 15 15:47:23 PDT 2011


On 5/15/11 2:20 PM, Rick Waldron wrote:
> Thanks Brendan, I was looking for something that was representative of
> Boris's use-case

A typical example is an extension wanting to associate some state with a 
DOM element or a Window without polluting the DOM.  For example, Adblock 
Plus wants to store some state per-element so that it knows when it's in 
the middle of unblocking something so it'll allow the load through for 
that one thing only.  Firebug wants to store some state per-window (e.g. 
script bodies, etc) and discard it when the window goes away.

Right now the latter is solved through Gecko-specific notifications that 
Firebug listens for and the former is solved in ways that are a bit 
hacky and error-prone, as I recall.  Both would be solved with an API 
that lets you use the element or window as the key to look up the data 
of interest but doesn't keep the element or window alive.

Note that JS libraries on the web have some similar issue; right now 
they use various sorts of hacks to decorate elements with properties 
whose names are "not likely" to collide with other stuff...

-Boris


More information about the es-discuss mailing list