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:
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:
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There are some issues in this article. I can guess some of those come from the notation you've used?
That's correct for
map
andfilter
, butforEach
has now been added toNodeList.prototype
.It won't. Elements in a
NodeList
are objects of classNode
, and that's what matters, becausecompareDocumentPosition
is defined in the prototype of theNode
class. The classNodeListItem
doesn't exist in JavaScript, and anyway spreading the node list into an array, or usingArray.from
on it, won't change the class of its items: they will still beNode
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:That won't work. First, it's just
Array
, whileObject.Array
doesn't exist. Then, asArray
is just a constructor, it's the methods fromArray.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:The object returned by
map
would not be aNodeList
, but anArray
. (That makes a lot of sense, as you could get a list of whatever you want withmap
, but aNodeList
is purely a list of... nodes.)Other methods like
push
orsplice
modify the source object itself, butNodeList
's are immutable in JavaScript so those methods would throw aTypeError
.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: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.