I am not an expert in English, but reading about ES6 I have come across on several occasions that they mention Array-like objects (especially when I read about Map , Set , WeakMap , WeakSet ), as in the following context:
Other collections support for-of too
for–of
is not just for arrays. It also works on most array-like objects, like DOM NodeLists.
From what little I understand, it looks something like what jQuery returns when they query a list of elements with (example) $('a')
.
I understand that an Array is already an object, but what does the term Array-like object refer to ? How do I create one? What is the purpose compared to an array?
An array-like object is an object that resembles an array because it represents a set of elements and has a property
length
, however it does not directly have the very useful methods of , , etc.Array.prototype
sort
filter
A very common example of this type of object is
arguments
that it refers to the arguments that are sent to a function:Since we cannot directly invoke any method,
Array.prototype
what we do is first convert it to an array:The method
slice
has the ability to work with array-like objects and therefore conversion is possible.Other examples are DOM objects obtained through methods like
document.getElementsByClassName
, known asNodeList
. This is an alternative way to convert to array:Ideally you don't have to create this type of objects, it will always be easier to use arrays when you need a collection of elements, this makes it easier to integrate a program into a project, since if all the functions use arrays there is no need to do conversions. You just have to be aware of what they are and how they differ from arrays.