DEV Community

Using A NodeList as an Array: A Practical Use for Object Composition

Julián Herrera on August 28, 2017

Using A NodeList as an Array The Scenario: I had queried a bunch of tags, using querySelectorAll(), and received a NodeList i...
Collapse
 
bcallaars profile image
Joseph Callaars • Edited

I like this solution. Only very recently I came across this problem myself as well (I mainly only do back end), and I solved it through [].forEach.call(NodeListHere, function () {}) as I didn't want to change original object.

Collapse
 
maxart2501 profile image
Massimo Artizzu

There are some issues in this article. I can guess some of those come from the notation you've used?

NodeLists are like arrays ... however, try using .map, or .filter, or .forEach on one.

That's correct for map and filter, but forEach has now been added to NodeList.prototype.

nodeElementInTheArray.compareDocumentPosition(anotherNodeElementInTheArray)

This will error out because the argument is not a true NodeListItem.

It won't. Elements in a NodeList are objects of class Node, and that's what matters, because compareDocumentPosition is defined in the prototype of the Node class. The class NodeListItem doesn't exist in JavaScript, and anyway spreading the node list into an array, or using Array.from on it, won't change the class of its items: they will still be Node instances.

The question remain, here: why do you need a NodeList for? In all my career, when I needed it, I've always been happy to have an array instead, back when in ES3 I had to do this:

var nodearray = Array.prototype.slice.call(nodelist);
Object.assign(*NODELIST*, Object.Array)

That won't work. First, it's just Array, while Object.Array doesn't exist. Then, as Array is just a constructor, it's the methods from Array.prototype that you'd probably want. But then again, Object.assign copies the properties from the source to the destination object, but only the enumerable ones. Now, map, filter and all the others are not enumerable, so your node list won't get any additional property.

Moreover, there's another concern here. Even if you do copy a method like map to your node list, like this:

nodelist.map = Array.prototype.map;
// or
nodelist.map = [].map;

The object returned by map would not be a NodeList, but an Array. (That makes a lot of sense, as you could get a list of whatever you want with map, but a NodeList is purely a list of... nodes.)

Other methods like push or splice modify the source object itself, but NodeList's are immutable in JavaScript so those methods would throw a TypeError.

One way to actually have the methods from arrays in a node list, and have it still as an instance of NodeList, is to replace its prototype:

nodelist.__proto__ = Array.prototype;

But this, of course, is not object composition, and it's also generally frowned upon as mutating an object's prototype has quite some overhead.