Why does Array.from accept non-iterable arraylikes?
waldron.rick at gmail.com
Mon Jun 24 09:43:03 PDT 2013
On Mon, Jun 24, 2013 at 11:54 AM, Jason Orendorff <jason.orendorff at gmail.com
> According to , 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.
> Certainly the idea is for all DOM arraylikes to be iterable. So what
> is this for?
> 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.
It's my pleasure and I'm glad you're finding them useful as a resource
during implementation :)
The problem is that I can't really record my own input... while I'm
talking, no one is taking notes. When I'm done talking, I'm taking notes.
The example Brendan wrote on the board was to illustrate that a "helper"
function could be used to turn arraylikes into iterables before being
passed to Array.from. I said "no" because it contradicts the agreement made
a few lines prior.
While all builtin arraylikes are also iterable now, arraylikes created by
non-built-in (assuming we both use "built-in" to refer to objects defined
by ES) may not implement iterator protocol. Array.from, like
Function.prototype.apply, will allow those objects to be turned into an
array, eg. NodeList, DOMTokenList or jQuery objects. A cowpath that
Array.from was intended to clear and pave was
.slice.call(arrayLike)/Array.prototype.slice.call(arrayLike). Also, web
devs will be able to use a polyfilled Array.from in code that must run in
browsers that don't/won't support spread (ie. older non-updating browsers).
Hopefully this helps to clarify
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss