Why does Array.from accept non-iterable arraylikes?

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Jun 24 09:41:21 PDT 2013


On Jun 24, 2013, at 9:07 AM, Erik Arvidsson wrote:

> I'm with Jason here.
> 
> The only argument I can vaguely remember is that people want to be
> able to use these on old browsers without @@iterator? But I don't see
> why a polyfill could not tag things with __iterator__ or something
> else?

I think the other argument is that the Array iteration methods historically accept anything that vaguely looks "array-like".

If you can say:

Array.prototype.forEach({0:1,1:1,2:2,length:3}, n=> console.log(n));

why can't you say:

Array.from( {0:1,1:1,2:2,length:3}).forEach(n=>console.log(n));

or:

for (let n of Array.from( {0:1,1:1,2:2,length:3}))  console.log(n);

Allen





> 
> On Mon, Jun 24, 2013 at 11:54 AM, Jason Orendorff
> <jason.orendorff at gmail.com> wrote:
>> According to [1], Array.from will first try treating the argument as
>> an iterable, then as an arraylike. This is much better than just
>> arraylike. The committee considered making it iterable only, but
>> decided against it. The rationale recorded in the notes is:
>> 
>>    RW: No.
>> 
>> Rick, can you expand on that a bit? :-)
>> 
>> I propose changing it to iterable only. All builtin arraylike objects
>> are iterable now, including arguments, per the same discussion.[1]
>> Certainly the idea is for all DOM arraylikes to be iterable. So what
>> is this for?
>> 
>>  [1]: https://github.com/rwldrn/tc39-notes/blob/master/es6/2012-11/nov-29.md#revisit-nov-27-resolution-on-iterables-in-spread
>> 
>> -j
>> 
>> P.S. Rick: Thanks again for taking notes at the meetings. They're
>> indispensible. I've cloned the repo; grepping the notes has been super
>> useful too.
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
> 
> 
> 
> -- 
> erik
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 



More information about the es-discuss mailing list