array like objects

P T Withington ptw at pobox.com
Tue Dec 15 04:51:04 PST 2009


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)))) {
      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