array like objects

Breton Slivka zen at zenpsycho.com
Mon Dec 7 18:34:43 PST 2009


On Tue, Dec 8, 2009 at 1:29 PM, Breton Slivka <zen at zenpsycho.com> 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 microsoft.com> 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