array like objects

Mike Samuel mikesamuel at gmail.com
Mon Dec 7 18:41:51 PST 2009


2009/12/7 Allen Wirfs-Brock <Allen.Wirfs-Brock at microsoft.com>:
> 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.

Most of the code snippets I've seen use it to decide how to iterate
over elements and whether to produce an array or Object as output, not
to decide whether it's appropriate to apply Array generics.

The each function from http://code.jquery.com/jquery-latest.js does
  if ( args ) {
    if ( length === undefined ) {
// iterate using for in
    } else
      for ( ; i < length; )

And then any library routines that delegate to each to build an output
inherit the same definition.
I

>
>
> 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> 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
> https://mail.mozilla.org/listinfo/es-discuss
>
>


More information about the es-discuss mailing list