array like objects

On Sat, Dec 12, 2009 at 2:59 PM, Mark S. Miller <erights at google.com> wrote:
> On Sat, Dec 12, 2009 at 12:29 PM, Garrett Smith <dhtmlkitchen at gmail.com>
> wrote:
>> Object.prototype.propertyIsEnumerable does not consider properties in
>> the prototype chain. In effect, propertyIsEnumerable is a test to see
>> if an object has *own* property that is also enumerable.
>> At the end of
> <http://wiki.ecmascript.org/doku.php?id=conventions:isarraylike> I have
> added the issue:
>> Unfortunately, the propertyIsEnumerable test above, despite the name,
> actually tests whether a property is both enumerable and own. This means the
> above predicate will reject objects that inherit a non-enumerable length
> property. I can think of no reliable and portable ES3R test which tests only
> property enumerability, except by running a for-in loop and testing the
> output. Doing so would kill the bounded-by-constant time virtue.
>> Are we really this stuck? Can anyone think of a reliable, portable, and fast
> ES3R test that tests whether a property is enumerable, whether it is
> inherited or not?
>
Not stuck. Why do you care if |length| is enumerable?
If a standard |for| loop is used, it doesn't matter. Why anyone would
want to use |for in| for something that is arrayLike?
Can an arrayLike object have other properties? If you're talking about
a DOM collection, you can be that it will have other properties and
probably a mix of enumerable and non-enumerable proprietary
properties.
A DOM collection (NodeList, for example) might very well have a length
property that shows up in the body of a for in loop. There is no
standard that specifies one way or another if a NodeList's length
property should be enumerable. I can't see why anyone would care,
either.
javascript: for(var p in document.childNodes) if(!isFinite(p)) alert(p);
Firefox 3.5, Safari 4, Chrome 2:
"length"
"item"
Opera 10:
"length"
"item"
"namedItem"
"tags"
IE7
"tags"
That example alone shows a few good reasons why using a for in loop
would be a bad choice. In superficial testing in a few browsers, the
length property showed up in the body of a for in loop. Other
properties will show up, depending on the implementation and property
name. Other collections will likely have even more properties.
if a DOM collection is considered arrayLike, then using for in loops
over arrayLike can be considered to be a reckless, error-prone
concept.
OTOH, If indexed properties are got off the object, as with your
standard for loop, the enumerability of - length - is irrelevant. So
again: Why do you care if |length| is enumerable?
Garrett