Array subclassing, .map and iterables (Re: Jan 30 TC39 Meeting Notes)

Claus Reinke claus.reinke at
Sat Feb 9 02:16:23 PST 2013

I am trying to understand the discussion and resolution of
'The Array Subclassing "Kind" Issue'. The issue (though not its
solution) seemed simple enough

    class V extends Array { ... }
    m = (new V()).map(val => val);
    console.log( m instanceof V ); // false :(

and I was expecting solutions somewhere along this path:

1. .map should work for Array subclasses, preserving class

2. .map is independent of Array and its subclasses, there are
    lots of types for which it makes sense (Sets, EventEmitters, ..)

3. there should be an interface Mapable, implemented by
    Array and its subclasses, but also by other relevant classes,
    such that

    class M implements Mapable { ... }
    m = (new M()).map(val => val);
    console.log( m instanceof M ); // true

    (in typed variants of JS, this would call for generics, to 
    separate structure class -supporting map- from element 
    class -being mapped)

Instead, the accepted approach -if I understood it correctly-
focuses on conversion and iterables:

    Array.from( iterable ) => Array.from( iterable, mapFn )

such that

    SubArray.from( iterable, val => val ) instanceof SubArray

This seems very odd to me, because

- it introduces a second form of .map, in .from

- instead of limiting to Array, .from-map is now limited to iterables
    (it would work for Set, which is really OrderedSet, but it wouldn't 
    work for WeakMap)

- it doesn't address the general problem: how to inherit structural
    functionality (such as mapping over all elements or a container/
    iterable) while preserving class

With a general solution to the issue, I would expect to write

    SubArray.from( iterable ).map( val => val ) instanceof SubArray

while also getting

    new Mapable().map( val => val ) instanceof Mapable

Could someone please elaborate why the committee went with 
an additional map built into structure conversion instead?


PS. What about array comprehensions and generator expressions?

More information about the es-discuss mailing list