Why is JavaScript NodeList immutable?

I recently encountered the fact that the childNodes property of an element returns a NodeList, not an array. Now I understand that NodeList is intended for a live collection of elements, but I do not understand why this precludes the use of methods such as indexOf, or even push.

Can someone explain why the only thing you can do with NodeList is to index it?

+3
source share
2 answers

Because the way it was indicated. The DOM API was developed separately from JavaScript. The fact that it NodeListhas some common aspects with JavaScript arrays ( lengthand indexing) is just ... well, it's probably not a coincidence, but a by-product of the inputs to the design process. Remember that JavaScript is not the only language that has DOM bindings.

You can easily affect the content NodeListusing the DOM API:

... or, of course, your favorite JavaScript library.

+4
source

I got the impression that this is the case, that you will need to use the proper DOM mechanisms to modify the DOM structures.

When considering compatibility issues, it is probably safer to use DOM methods:

http://reference.sitepoint.com/javascript/NodeList

0

All Articles