Set length property

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Feb 14 19:11:22 PST 2012


On Feb 14, 2012, at 6:34 PM, Mark S. Miller wrote:

> On Tue, Feb 14, 2012 at 1:20 PM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:
> 
> I don't want to go looking anything up right now, but I have lots of
> memories of things looking for the presence of "length" to denote an
> array-like.
> 

In ES5:
Many of the the Array.prototype methods, including all of the "Array extras" use length based iteration of "array indexed"  properties and will work with all "array-like" objects.
apply uses length based iteration of "array indexed" properties
The arguments object is "array-like" (length property and array indexed element properties)
The match object return by RegExp exec is "array-like"
String objects are "array-like"


In the ES6 draft, so far
the spread operator uses length based iteration of "array indexed" properties

In all of the above contexts, "array-like" means that the ToInteger value of the length property is one more than the largest valid "array index" property (in ES5 a Uint32 value).  Array-like processing of an object means iterating over it "array index" properties from 0 to length-1.  Where or not "holes" are skipped or processed (as undefined) depends upon upon the specific algorithm.

Object without a length property are seen as having a length value of 0.  (ToInteger(undefined) yields 0) and hence always perform 0 iterations 


> If someone does look for examples, this would be helpful. The issue is "array-like in what ways"? Other than being iteratable, Sets and Maps aren't array-like in any other way I can think of. And "iteratable" is new with ES6 so that can't be the commonality. OTOH, functions have a "length" and they aren't array like in any way I can fathom. The possible confusion with functions may have already deterred people from duck typing on "length".

If you used a function object in a context where an "array-like" is required is will be treated as having "holes" for its array indexed properties up to its length value -1. 


Allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120214/f2d0aa2f/attachment.html>


More information about the es-discuss mailing list