Overriding Map/etc with get/set hooks?

Brendan Eich brendan at mozilla.com
Wed May 22 03:40:08 PDT 2013


Tab Atkins Jr. wrote:
> On Tue, May 21, 2013 at 4:19 AM, Brendan Eich<brendan at mozilla.com>  wrote:
>> So Tab: why do you want to abuse Map instead of make a custom class?
>
> Restating from my earlier post:
>
> 1. when someone asks "is this Map-like?" in an appropriately idiomatic
> JS way, they get a "yes" answer.
> 2. when someone adds a new function to Maps in an appropriately
> idiomatic JS way, the method also applies to this object
> 3. when JS expands the set of built-in methods for Map, it also gets
> applied to this object without me having to update my spec
> 4. for all the existing Map methods, I get identical/equivalent methods
> without having to manually redefine every single one of them

There aren't many Map methods. Write your own workalikes if you need 
'em. Done.

> All of these follow from the basic statement that *this is a map*,
> because it clearly is.

Circular arguments won't help here. A Map doesn't have 
throw-on-certain-key behavior; a Map doesn't act at a distance on other 
data without metaprogramming.

You've falling into a nominal typing trap. JS doesn't have a Map that 
everyone must bend or break to suit quasi-Map-like use-cases, any more 
than Array must be directly bent (or hooked with @@coerceKey trash) to 
be a NodeList.

Make a different data structure (it can subclass if you insist; it could 
instead use Map internally).

/be


More information about the es-discuss mailing list