Use cases for WeakMap

Brendan Eich brendan at mozilla.com
Mon May 16 00:28:28 PDT 2011


On May 16, 2011, at 12:10 AM, Erik Corry wrote:

> 2011/5/16 Brendan Eich <brendan at mozilla.com>:
>> On May 15, 2011, at 11:53 PM, Erik Corry wrote:
>> 
>>> 2011/5/16 Brendan Eich <brendan at mozilla.com>:
>>>> This is a good point too. Not sure we've considered a value -> value map carefully yet.
>>> 
>>> A value->anything map is pretty easy to do with a normal JS object.
>>> 
>>> function get(value) {
>>>  if (typeof(value) == 'number') return this["NUM" + value];
>>>  if (typeof(value) == 'string') return this["STR" + value];
>>>  ...
>>> }
>> 
>> Where's the 'object' case (excluding null)?
> 
> Elided for clarity :-)  It can be implemented with private names or
> WeakMaps.

Oh, ok -- you wrote "normal JS object" and that seemed to preclude new stuff.

Yes, we can make a value -> value map as a library abstraction, but it's clunky. It has to use two kinds of maps under the hood. People shouldn't necessarily have to reinvent or discover or curate it and download those bytes all the time.


> My point was we don't need to think about maps with values
> as keys.  We have that already.

Your use of "normal JS object" and present tense to refer to either weak maps or private names (implemented via weak maps) is making me want to wait a few months and talk again :-/.

/be


More information about the es-discuss mailing list