Why does Array.from accept non-iterable arraylikes?

Jason Orendorff jason.orendorff at gmail.com
Wed Jun 26 15:10:58 PDT 2013


On Wed, Jun 26, 2013 at 12:15 PM, Dean Landolt <dean at deanlandolt.com> wrote:
> On Wed, Jun 26, 2013 at 12:15 PM, Jason Orendorff
> <jason.orendorff at gmail.com> wrote:
>> If we make the name just plain "iterator", there's a conventional
>> right answer for this: `typeof obj.iterator === "function"`.
>
> Yes -- my point about the schism is what libraries will decided to do when
> that test fails.

If the test returns false, then the object is not iterable.

> What if iterator is present but not a function? Do you walk
> the prototype chain anyway? Blow up? Punt and lookup an iterator directly
> based on a mapping with some type testing? [...]

Well, no, it should be treated like any other non-iterable object.

If JS were being designed green-field today, maybe some of us would
make array.length a symbol, not because it's "meta" (meaning, it is
used by some key builtins, notably including Function.prototype.apply)
and not because programmers might want to write a function that
accepts an argument that's either an arraylike object or a dictionary.
Those cases *do* exist, but it's just not a problem. Making .length a
symbol would be a mistake.

-j


More information about the es-discuss mailing list