Posted onEdited onInjavascript
,
array Symbols count in article: 358Reading time ≈1 mins.
What is array-like object?
Array-like object is an object that has a length property and indexed elements. For example, arguments is an array-like object.
Has indexed access to the elements and a non-negative length property to know the number of elements in it. These are the only similarities it has with an array. Doesn’t have any of the Array methods like push, pop, join, map, etc.
1 2 3 4 5 6 7 8 9
const arrayLike = { 0: 'a', 1: 'b', 2: 'c', length: 3, }; console.log(arrayLike[1]); // 'b' console.log(arrayLike.length); // 3 console.log(arrayLike.push); // Uncaught TypeError: arrayLike.push is not a function
What is the difference between array and array-like object?
类型
length属性
索引访问
有Array.prototype方法
Array
✔️
✔️
✔️
Array-like object
✔️
✔️
❌
Which type in JavaScript is array-like object?
There are many types in JavaScript are array-like object, including: