Use cases for WeakMap

Erik Corry erik.corry at
Mon May 16 00:43:03 PDT 2011

2011/5/16 Brendan Eich <brendan at>:
> On May 16, 2011, at 12:10 AM, Erik Corry wrote:
>> 2011/5/16 Brendan Eich <brendan at>:
>>> On May 15, 2011, at 11:53 PM, Erik Corry wrote:
>>>> 2011/5/16 Brendan Eich <brendan at>:
>>>>> 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.

This is where we disagree fundamentally.  I think we should give
people the lego bricks to build the abstractions they need.  You seem
to think we should give them all the abstractions  they are going to
need, ready built.

As for downloading all the time that's a caching/modules/libraries
issue which needs to be solved in other ways.  It has to be possible
to write composable modules in the language that are downloaded only
once for each time a new release is created.  If we can't solve that
then the temptation will always be to add every thinkable module to
the language and the result will be constant pressure for bloat.  I
regard the i18n changes currently going in as an example of that.

>> 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 :-/.

I didn't use 'normal JS object' in that sense.

The context was you talking about a value->value map.

I presented a value->anything map which was implemented as a 'normal object'.

You complained it wasn't an anything->anything map.

I said you could do that with either private properties or weak maps.
At that point you need both the 'normal object' and something else.

Perhaps we have a nomenclature problem with 'value'.  Do you regard
things that have typeof(o) == 'object' as values?

Erik Corry

More information about the es-discuss mailing list