This article: Traversing the DOM with filter(), map(), and arrow functions advocates such a use of Array.map that seems odd to me. More specifically, the author of the article claims the following piece of code is valid and actually better than the alternative Array.from(elements).map(...):
var elements = document.getElementsByClassName("bgflag");
BgFlags = Array.prototype.map.call(elements,
element =>
({
height: element.offsetTop,
bgsrc: element.dataset.bgsrc,
bgcolor: element.dataset.bgcolor,
size: element.dataset.size,
name: element.id,
image: parseInt(element.dataset.image)
})
);
This seems most suspicious to my untrained eye. We are calling Array.prototype.map on something that is not an Array. Unless it is somewhere explicitely stated that this is allowed, this smells like undefined behavior to me. I quickly scanned through the relevant MDN documentation, but couldn't find that such a usage is allowed there.
And yet, the author of the article underlines:
Even though map() is a function of Array.prototype, it works on any array-like iterable.
Is such a use valid, as he claims? If so, is this also the case with other Array.prototype.* functions, like filter, slice, maybe even pop, push, others?