typeof null

Brandon Benvie brandon at brandonbenvie.com
Thu May 10 01:05:19 PDT 2012

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);

The reason there will never be a single solution to these issue is that
> desirable behavior varies depending  upon what the programmer is trying to
> accomplish. Just using (possibly cross-frame) arrays an an example.  What
> is the programmer really asking when they say "is this object an Array".
>  Do the need to know whether it has array indexed properties and a length
> property? Do they need to know where it maintains the special Array length
> invariant? Do they need to know whether it implements it has properties
> that implement the array extra methods? Do they need to know that in
> inherits from the build-in array prototype object?  Or, do they just need
> to know whether they should serialize the object using [ ] instead of {  }
> notation
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120510/b7402c38/attachment-0001.html>

More information about the es-discuss mailing list