First, there is no Array.map
. The use case for the former variant is the following: You won't be able to invoke map
for example via document.querySelectorAll('div')[0].map
. That is to the fact, that document.querySelectorAll
returns a NodeList
and not an array and lacks the map method.
What makes one better than the other?
The question really is not which variant is better. It is about not being able to call map
on the NodeList.
Most functions within the array prototype are able to handle array-like objects as well (that is, objects whose keys are representations of numeric indices) when passed as thisArg
to call(or apply). Hence,
Array.prototype.map.call(array-like, mapFunction)` can map the NodeList just like an array.
There's two alternatives, however:
- Use Array.from:
Array.from(nodes, n => n.innerHTML)
introduced in ES6 and not supported in Internet Explorer. However, when that does not matter, I would prefer this.
- Use
Array.prototype.slice.call(nodes).map(n => n.innerHTML)
, which works similar (see how does Array.prototype.slice.call() work?) or stick with Array.prototype.map.call
.
According to your overall code, however, the usage of (1) seems perfectly fine and I, personaly, would prefer this.