array like objects [correction]

Mark S. Miller erights at google.com
Fri Dec 11 17:13:14 PST 2009


On Fri, Dec 11, 2009 at 4:33 PM, David-Sarah Hopwood <
david-sarah at jacaranda.org> wrote:

> David-Sarah Hopwood wrote:
> > Mark S. Miller wrote:
> >> function isArrayLike(obj) {
> >>   var len;
> >>   return !!(obj &&
> >>             typeof obj === 'object' &&
> >>             'length' in obj &&
> >>             !({}).propertyIsEnumerable.call(obj, 'length') &&
> >>             (len = obj.length) >>> 0 === len);
> >> }
> >>
> >> Since getting 'length' may have side effects, this is written a bit
> weird so
> >> that this get only happens after earlier tests pass.
> >
> > If you want to avoid side effects:
> >
> > function isArrayLike(obj) {
> >   if (!obj || typeof obj !== 'object') return false;
> >   var desc = Object.getPropertyDescriptor(obj, 'length');
>
> getOwnPropertyDescriptor, I meant.
>
>
Since there is no legacy issue with it, and we don't want to enable code to
make stability claims we can't enforce, we wish to keep
getOwnPropertyDescriptor meta. Also, for many ES5 objects that wish to be
considered array-like, their length will be an accessor property anyway.

I think I buy your argument that we simply shouldn't be testing the value of
length in this predicate. If we get rid of that last conjunct, all is well.



> >   if (desc) {
> >     var len = desc.value;
> >     return !desc.enumerable && (len === undefined || len >>> 0 === len);
> >   }
> > }
>
> --
> David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>


-- 
   Cheers,
   --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20091211/30931871/attachment-0001.html>


More information about the es-discuss mailing list