JS: Array-Like Object
What is a array-like object
A object such that is all of the following:
- has property names of consecutive non-negative integers {
"0"
,"1"
,"2"
, β¦}. - has a property
"length"
with a value that's the number of these properties. - is not a true array. That is,
Array.isArray( obj)
returnsfalse
.
[see Understand JS Array]
Difference between array and array-like object
- For true array, value of
length
will automatically change. - For true array, its parent is
Array.prototype
.
// create a array-like object let a1 = {0:"a", 1:"b" , length:2}; a1[2] = 99; console.log(a1.length); // 2 // length would be 3 if it's a true array // real array let a2 = ["a", "b"]; a2[2] = 99; console.log(a2.length); // 3
[see Prototype and Inheritance]
[see Array.prototype]
Check if a object is true array
Array.isArray( obj)
[see Array.isArray]
Where does array-like object came from
In DOM, method such as document.getElementsByClassName
return array-like objects.
function
arguments Object
is a array-like object.