array like objects [correction]

David-Sarah Hopwood david-sarah at jacaranda.org
Fri Dec 11 16:33:24 PST 2009


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.

>   if (desc) {
>     var len = desc.value;
>     return !desc.enumerable && (len === undefined || len >>> 0 === len);
>   }
> }

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 292 bytes
Desc: OpenPGP digital signature
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20091212/98411f5d/attachment.bin>


More information about the es-discuss mailing list