array like objects

Mike Wilson mikewse at
Fri Dec 11 02:27:00 PST 2009

I think Breton mentions something important here; the desire
to actually detect if something is an array or arraylike to
be able to branch to different code that does completely
different things for array[likes] and objects. If we just
provide a better generic iteration construct then this part
is lost, as I might even not be doing iteration in one of
the branches.

Certainly, solving this isn't easy, but it would be good if
there was some direction taken by the WG to provide a
standard way to detect arraylikeness without having to do
iteration, so JS runtimes, browser host objects and JS 
libraries can follow this precedent.

Best regards
Mike Wilson

Breton Slivka wrote:
> On Tue, Dec 8, 2009 at 1:29 PM, Breton Slivka 
> <zen at> wrote:
> > The one that I use is
> >
> > function isArrayLike(i){
> >    return (typeof i !=="string") && i.length !== void (0);
> > }
> >
> > It might not be perfect, but it allows me to make certain 
> assumptions
> > about the input that are useful enough. Keep in mind that 
> an Array may
> > have a length of 5, and all those values are undefined, so  
> {length:4}
> > could be used as a valid arrayLike, and that seems reasonable to me.
> >
> On Tue, Dec 8, 2009 at 1:18 PM, Allen Wirfs-Brock
> <Allen.Wirfs-Brock at> wrote:
> > 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.
> >
> >
> You've encapsulated here some of the reasoning behind my earlier test-
> Anything that passes my test is acceptable to any of the array
> prototype functions, but filters out two situations that I've found I
> didn't intend an object to be treated as an array. My usecase is
> overloaded functions that may also accept objects, and strings, and
> arraylikes, but I want a way to distinguish array-likes from objects
> and strings.

More information about the es-discuss mailing list