7

I get that since javascript allows numeric keys for objects, the existence of array-like objects is therefore technically possible, but why did they ever become common?

Maybe the thought was that these array-like objects don't just have numeric keys, e.g. arguments has the callee property, so they can't be proper arrays to accommodate those properties.

But in javascript, it's perfectly valid to treat an array as an object and use non-numeric keys:

var myArguments = []; 
myArguments[0] = 0; 
myArguments['callee'] = function(){console.log('callee')};

If the object is array-like and would benefit from having access to the functions it would otherwise inherit from the array prototype, what would be advantage of making it an array-like object instead?


Edit: in case it wasn't clear in my question, an array like object would be something like the arguments object that has sequential numeric properties starting from zero, and has a length property that is one less than the highest numeric key. It also doesn't inherit from the array prototype, and doesn't provide access to array methods.

martin
  • 1,102
  • 2
  • 12
  • 20
  • 2
    `array-like` - you have an array, nothing "like" about it - and like anything in javascript, arrays inherit from Objects, so non-numeric "property keys" are possible – Jaromanda X May 22 '17 at 05:59
  • 1
    It wasn't always possible to inherit from (sub-class) `Array`, so if you wanted to create such objects using a constructor function (with instances all linked to the same prototype) you couldn't use an actual `Array`. But you could still use `.call()` or `.apply()` to use (some) array methods on your array-like object, so... – nnnnnn May 22 '17 at 06:04
  • 1
    @nnnnnn That actually makes a lot of sense, especially for something like `HTMLCollection`, it's still a bit confusing for `arguments` though where it inherits directly from the Object prototype. – martin May 22 '17 at 06:24
  • "*why did they ever become common?*" - I would argue that they never became common. – Bergi Dec 12 '21 at 06:07

4 Answers4

1

I think the biggest advantage of using Array-Like object, is an object itself. An object has less overhead than an Array. Can read: Array vs. Object efficiency in JavaScript

The other advantage of using objects, Language does not have to create/allocate contiguous memory as in Array. The object is more dynamic in nature. The array has to create a memory block in advance. and update on overload.

Read more: How are the JavaScript Arrays internally resizing?

Another factor, I think lookup in a map or object is faster.

xdeepakv
  • 7,835
  • 2
  • 22
  • 32
  • "*The array has to create a memory block in advance.*" - no, it doesn't. The engine can optimise this in the same manner as it can for objects. – Bergi Dec 12 '21 at 06:06
0

There is no big advantage, as you can access objects and arrays in very similar ways, array[number] for arrays, and object.propery, and object["property"] for objects. Arrays are better for storing data, not functions, in my opinion, and objects are better for storing sets of functions, in my opinion. For example, arrays could store your answers in a memory game, but an object would store the command for the game, like start or end.

cs1349459
  • 911
  • 9
  • 27
-1

Edit: As @Bergi stated, I forgot to add length property to the object. Also, I realized that my answer was not relevant. So I updated it after a short research.

In Typescript an arraylike object is defined like below:

interface ArrayLike<T> {
  length: number;
  [n: number]: T;
}

There are two main examples of array-like objects: arguments and HTMLCollection. The reason for using an array-like object is to use an array as only a data container without any behavior. As you could realize, if you try to use, say forEach, on arguments object you will get a type error since it's not defined on it. If you strictly believe that the array you are creating is only to be used as a data container without any methods whatsoever, then go with array-like objects. However, important to notice that they are getting scarcer day by day especially after ES6.

-3

Actually Array in JS is an Object :)

As long as You need array, it is bad idea to create array-like objects, because JS engines optimize speed of arrays. Yet, its possible.

grzesiekgs
  • 463
  • 2
  • 11
  • 2
    But the question asks why it has been (relatively) common to create array-like objects that are not actual arrays, not whether it is a good idea. – nnnnnn May 22 '17 at 06:15