Why does Array.from also take a mapFn?

Domenic Denicola domenic at domenicdenicola.com
Mon Jun 24 09:42:22 PDT 2013

From: Allen Wirfs-Brock [allen at wirfs-brock.com]

> My recollection is that we first discussed that the existence of Array.from make this issue somewhat less important because, just as you point out, .from can be used in conjunction with anything that produces an Iterable such as V.from(v.map(val => val * 2))

> That led to further discision of that usage and we got into things like the last example:
> ```js
> // Turn an array of nodeNames into NodeList of nodes
> NodeList.from( ["div"], node => document.createElement(node) );
> ```

I think I must be missing something. Why is this superior to

NodeList.from(["div"].map(node => document.createElement(node));

which you gave as an example just a few lines above? In other words, why does `Array.from` accept a second parameter at all?

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.

More information about the es-discuss mailing list