What is the advantage of nodeList
s and arguments
being just "array-like"?

- 14,597
- 32
- 109
- 229
-
Vote-to-close/downvote please comment why so I can refine and improve my question. It doesn't help the SO community to disapprove with anonymous silent clicks. – 1252748 Dec 05 '14 at 04:48
-
Can you not see close vote choices on your own question? The close reason that was picked explains it pretty well I think. – BoltClock Dec 05 '14 at 04:53
-
@BoltClock No I was not aware of that feature. But there was a downvote that was retracted between the time I started the comment and the page updated. That is what my comment was mainly aimed at. – 1252748 Dec 05 '14 at 05:19
2 Answers
In each case, the object has some behaviors that an actual array could not have.
In the case of NodeList
, some node-lists are "live" collections, meaning that even after creation, they continue to reflect later changes to the underlying DOM. (For example, if node
is a node, then node.childNodes
is a node-list that always contains its current children.)
In the case of arguments
, when you assign to its elements, this actually assigns to the corresponding local variables. For example, this function always returns 3
, no matter what argument is passed in:
function returnThree(i) {
arguments[0] = 3;
return i;
}
(You can even pass arguments
as an argument to another function, and let it assign to your local variables!)
(Note: Whether these behaviors are actually "advantages" is perhaps a matter for debate.)

- 175,680
- 26
- 273
- 307
-
1Nitpick, but being able to assign to the elements of `arguments` does not preclude it being a "true" array. – Dec 05 '14 at 04:50
-
@torazaburo: Well, obviously. I think you missed the second half of that sentence! – ruakh Dec 05 '14 at 04:51
-
@torazaburo: Since it seems that you're not the only person who didn't understand that, I've now expanded it and added an example. – ruakh Dec 05 '14 at 06:36
-
Thanks for the clarification. Actually, I do understand about how the `arguments` array proxies the arguments. What I was trying to say was, there is absolutely no reason why that special behavior is not compatible with having `arguments` being a normal Array in the sense that it can take a `.forEach`, for example. Well, it's all a bit of a moot point anyway since ES6 spread operators essentially eliminate the need for this. – Dec 05 '14 at 16:32
-
1@torazaburo: It could have a `.forEach` method, sure -- and every other method that arrays have -- but I don't see how it could actually *be* an array. Arrays don't have that kind of aliasing. – ruakh Dec 05 '14 at 18:17
arguments
arguments
not being a true array is a historical artifact. Although as pointed out in another answer, it has the callee
property, and can be proxied onto the actual arguments, in and of itself that would not have prevented it from being a true array. For more discussion, see Why isn't a function's arguments object an array in Javascript?. It is also worth pointing out that the toString
method on arguments
behaves differently than an array.
NodeList
NodeList
is not part of JS per se; it's a DOM data interface definition, which is designed to be used from many languages and environments. If it were to be an array in JS implementations, one would probably want it to be of a type subclassed from Array, but JS does not support subclassing Array very well. Also, in such cases, we would have the slightly odd situation of a DOM datatype which in JS differs (extends) the definition of the specified data interface. It is true, as pointed out in other answers, that NodeList
may be "live", but again, that in itself would not prevent it being a true array.
ES6 provides functions such as Array.from
which will ease some of the pain.

- 1
- 1