array like objects

Mike Samuel mikesamuel at
Tue Dec 15 10:59:05 PST 2009

2009/12/15 P T Withington <ptw at>:
> On 2009-12-08, at 13:10, Mike Samuel wrote:
>> All true.  And yet it is not uncommon.  See the bottom of this email
>> for a quick survey of a number of libraries' uses of the array-like
>> concept.
> FWIW, Here is the (separately/simultaneously invented) definition from OpenLaszlo:
>  function isArrayLike (obj:*):Boolean {
>    if (obj && ((obj is Array) || (obj['length'] != (void 0)))) {

Please excuse my ignorance, but is "is" equivalent to "instanceof" and
is the use of both obj['length'] and obj.length significant?

>      var ol = obj.length;
>      return ((typeof(ol) == 'number' || ol is Number) &&
>              ((ol|0) === ol) &&
>              (ol >= 0));
>    }
>    return false;
>  }
> This only shows up in the debugger, and it is used to heuristicate when inspecting an object whether to portray it as an Array (possibly with non-numeric properties), or as an Object.  It's an acknowledgement that users do create objects that they think of as arrays.  OTOH, we don't do any magic beyond that.  If a user intends an object to be treated as an array, they use Array operations on it (including `for`, not `for in`).
> I once had the vain hope that I could say:
>  function MyArray () {}
>  MyArray.prototype = [];
> to create my own subclasses of Array.  That might have lessened the need for isArrayLike.

More information about the es-discuss mailing list