On May 10, 2012, at 1:05 AM, Brandon Benvie wrote:
> On this tangent, I personally almost always mean "isArrayish" in this very
> common situation. That is, indexed with a length such that almost all
> Array.prototype methods would work on it out of the box. The number of
> arrayish interfaces provided by host environments has continued to grow. It's
> the epitome of generic, easily extended, usable, and efficient interfaces.
> Sometimes duck typing goes from "walks like a duck" to "is a vertebrate and
> is a bird and can fly and is found in sub-tropical areas and walks like a
> duck and quacks like a duck and can mate with ducks (offspring optional)".
>
>
>
> The following is the shortest check I can think of to accomplish this goal
> accurately. It isn't guaranteed to work with sparse collections but that are
> rare aside from built-in arrays themselves.
>
> function isIndexed(o){
> return Boolean(o) && hasOwn(o, 'length') && hasOwn(o, o.length - 1);
> }
Note that the "generic" Array.prototype methods get away without have to do a
"isArrayish" classification test. In general, the Array.prototype methods are
specified such that all objects appear "arrayish" to them. They do this by
accessing the "length" property and doing a ToUint32 conversions on the result.
ToUint32 produces 0 for values that can't be converted into integers such as
the undefined that was returned if the "length" didn't exist. So an object
without a length property is considered to have a length of 0. The algorithms
all iterate over indices in the interval 0 to length-1, so no "array elements"
get processed for such objects.
Allen
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss