array like objects

Allen Wirfs-Brock Allen.Wirfs-Brock at microsoft.com
Mon Dec 7 18:18:03 PST 2009


Curiously, I don't believe any of the "generic" functions for arrays and array-like objects in section 15.4.4 actually depend upon such an "array-like" test. They all simply use ToUint32 applied to the value of the length property.  If the length property doesn't exist or its value isn't something that an a convertible representation of a number, the value 0 is used as the length and not much happens.  By this definition, all objects are essentially array-like but many have no array-like elements.

It make one wonder why if this definition works satisfactory for the built-ins it should be used for all array-like situations.

Allen

From: es-discuss-bounces at mozilla.org [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Yehuda Katz
Sent: Monday, December 07, 2009 5:40 PM
To: mikesamuel at gmail.com
Cc: es-discuss
Subject: Re: array like objects

Your strawman would support:

{0: "strawman", length: 0}

A better definition might be:

   o is an array like object if o[[Get]]('length') returns a Number one greater than the largest numeric property, or 0 if no numeric properties exist.

Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325

On Mon, Dec 7, 2009 at 5:33 PM, Mike Samuel <mikesamuel at gmail.com<mailto:mikesamuel at gmail.com>> wrote:
It occurred to me after looking at the proxy strawman that it might
help to nail down what "array-like" means in future drafts.  It isn't
directly related to the proxy stuff though so I thought I'd start a
separate thread.

I've seen quite a bit of library code that does something like
  if (isArrayLike(input)) {
    // iterate over properties [0,length) in increasing order
  } else {
    // Iterate over key value pairs
  }
but different libraries defined array-like in different ways.
Some ways I've seen:
   (1) input instanceof Array
   (2) Object.prototype.toString(input) === '[object Array]'
   (3) input.length === (input.length >> 0)
etc.

The common thread with array like objects is that they are meant to be
iterated over in series.
It might simplify library code and reduce confusion among clients of
these libraries if there is some consistent definition of series-ness.
This committee might want to get involved since it could affect
discussions on a few topics:
  (1) key iteration order
  (2) generators/iterators
  (3) catchall proposals
  (4) type systems

One strawman definition for an array like object:
   o is an array like object if o[[Get]]('length') returns a valid
array index or one greater than the largest valid array index.

The need to distinguish between the two in library code could be
mooted if for (in) on arrays iterated over the array index properties
of arrays in numeric oder order first, followed by other properties in
insertion order, and host objects like NodeCollections followed suit.
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org<mailto:es-discuss at mozilla.org>
https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20091208/9a3d9dee/attachment-0001.html>


More information about the es-discuss mailing list