array like objects

Mike Samuel mikesamuel at gmail.com
Sat Dec 12 17:29:30 PST 2009


2009/12/12 Garrett Smith <dhtmlkitchen at gmail.com>:
> On Fri, Dec 11, 2009 at 12:08 PM, Mike Wilson <mikewse at hotmail.com> wrote:
>> Mark S. Miller wrote:
>>
>> If we're looking for a convention that is
>> * does not admit any legacy ES3R non-array non-host objects (to prevent
>> false positives)
>
> No native ES objects?
>
>> * does easily allow ES5 programmers to define new array-like non-array
>> objects
>> * takes bounded-by-constant time (i.e., no iteration)
>
> *What* takes bounded-by-constant time? The object's existence would
> not take any time.
>
>> * is a reasonably compatible compromise with the existing notions of
>> array-like in legacy libraries as represented by previous examples in this
>> thread
>
> Can you please clarify what the problem area is a little more?
>
>> then I suggest:
>> function isArrayLike(obj) {
>>   var len;
>>   return !!(obj &&
>>             typeof obj === 'object' &&
>>             'length' in obj &&
>>             !({}).propertyIsEnumerable.call(obj, 'length') &&
>>             (len = obj.length) >>> 0 === len);
>> }
>>
>
>  Is looks like "array like" is defined as an an object where "length"
> is non-enumerable and numeric. What about [[Get]], [[HasProperty]] for
> numeric property names?

I prefer "integral and non-negative" to "numeric."
As Brendan pointed out, the definition of uint32 already matches up
with length as specified for arrays.
He also points out that there in no limit on string length, but if
ulp(length) > 1 then you run into a lot of problems, so there is a
practical limit on length of 2**53.

I think any of the following definitions of length would be fine
  * uint32: (x === (x >>> 0) && isFinite(x))
  * non-negative integer: (x === +x && !(x % 1))
  * non-negative integer i such that ulp(i) <= 1: (x === +x && !(x %
1) && (x - -1) !== x)

> And why must the - length - property be an *own* property? Why could
> length not be a getter in the prototype chain?
>
> Indeed many Mozilla DOM collections work this way:
>
> javascript: var cn = document.links; alert([cn.length,
> ({}).hasOwnProperty.call( cn, "length")]);
>
> Mozilla elerts "80, false"
>
> Garrett
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list