Why does Array.from also take a mapFn?

Jason Orendorff jason.orendorff at gmail.com
Tue Jun 25 07:46:36 PDT 2013


On Mon, Jun 24, 2013 at 12:01 PM, Rick Waldron <waldron.rick at gmail.com> wrote:
>> Another question that I don't think got an answer, or if it did I was
>> unable to understand it. Why is "map" singled out for `Array.from` instead
>> of, say, "filter"? It seems arbitrary.
>
> It's not at all arbitrary: filter isn't an operation used to change the
> value of the items in the returned iterable.

Oh, I didn't get that Array.prototype.filter and the rest are going to
be subclass-friendly! Hmm. I want to love this change, but I'm
ambivalent. Typed arrays are fixed-length, and DOM NodeLists aren't
even constructible. :-\

NodeList is currently experimentally specified to be `[ArrayClass]`,
which would cause NodeList.prototype to inherit from
Array.prototype.[1][2] NodeLists would have .filter(). This is
awkward: with the ES5 algorithm, `nodelist.filter(pred)` will work,
returning a plain Array; but the proposed subclass-friendly algorithm
will try to create a NodeList---and throw an exception.

  [1]: http://dom.spec.whatwg.org/#interface-nodelist
  [2]: http://dev.w3.org/2006/webapi/WebIDL/#ArrayClass

If (as I am beginning to suspect) it just makes no sense for NodeList
to be a subclass of Array, we should urgently figure out some kind of
recommendation for such cases. DOM has more than just the one.

-j


More information about the es-discuss mailing list