12

It's very common in Javascript to come across Array-Like objects with some similarities to the build in Array type but without all of its methods or features. So much so that there are some tricks to convert Array-Like objects to "real" arrays for further manipulation.

This is even mentioned in Javascript: The Definitive Guide.

The questions is why is this pattern so common? Why not prefer the built-in Array type in all these cases?

elasticrat
  • 7,060
  • 5
  • 36
  • 36
  • Possibly a related question: http://stackoverflow.com/questions/1971389/array-like-object-in-javascript – Andrew Whitaker Nov 22 '10 at 00:36
  • For example: `arguments` is designed to be unmutable. That would not be the case if it was an array. – jwueller Nov 22 '10 at 00:44
  • 1
    @elusive, the `arguments` object is mutable, you can change the value of its properties. The index-named properties are *linked* to the function's FormalParameters, if you change the value of `arguments[0]`, the change will be reflected on the first named parameter of the function. Strict mode on ECMAScript 5, introduce some restrictions, but the object is still mutable. – Christian C. Salvadó Nov 22 '10 at 01:05
  • @CMS: You are right. Made a mistake here. It is _very_ early in the morning. Excellent answer, by the way. +1! – jwueller Nov 22 '10 at 01:10

1 Answers1

7

Well, speaking about core Javascript objects, the arguments is a good example to talk about.

In this case it has been an array-like object since the beginning, appeared in the ECMAScript 1th Edition Specification already as a simple object.

Why? I think at that time there were only four built-in Array methods, and maybe the designer didn't think it worthed too much, later the change was proposed, but Microsoft (part of the TC39 committee) didn't approved the change, the fear of breaking the web has always been present.

Now going to host objects, the DOM, NodeLists come to my mind, I think they didn't wanted to use the native Array type due the dynamic behavior of these objects.

NodeLists generally are live objects, their structure is reflects any change on the underlying DOM structure...

Personally I like array-objects, because they are really lightweight, before ECMAScript 5, there were a lot of restrictions in core methods, regarding the usage of user-defined array-like objects.

For example, the apply method of function objects, in ECMAScript <= 3, allowed only either a real array or an arguments object as its second argument, now in ES5, the following is possible:

var arrayLike = {0: 'hello ', 1:'world', length:2};
(function (a,b) { alert(a+b); }).apply(null, arrayLike);

See also:

Community
  • 1
  • 1
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • I suspect that the primary objection to turning `arguments` into an `Array` is that it would make an use of `arguments` *even harder* to optimize than it already is. – Gabe Nov 22 '10 at 01:24
  • 2
    @Gabe: Well, in the last discussions before ES5 got accepted, optimization wasn't the issue, the problem was backwards-compatibility, specifically with the `toString` and `toLocaleString` methods. The change even got to the draft state, but wasn't really straight-forward, they used an `Array` object, but defined two own properties (`toString` and `toLocaleString`) pointing to the methods on `Object.prototype`. After some time the change was dropped out from the ES5 draft. At the end, the optimization problems regarding the `arguments` object are solved on ES5 strict mode. – Christian C. Salvadó Nov 22 '10 at 05:08