Why does Array.from accept non-iterable arraylikes?

Jason Orendorff jason.orendorff at gmail.com
Tue Jun 25 14:17:09 PDT 2013


On Tue, Jun 25, 2013 at 3:19 PM, Brandon Benvie <bbenvie at mozilla.com> wrote:
> It's not just about backward compatibility, but also usability. Mandating
> the addition of a non-symbol property in order to work with a protocol (in
> this case the iterator protocol) is not good.

I don't buy this. Dynamic languages are full of protocols -- any time
a function calls a method on an argument, there's a protocol involved.
Duck typing.

Now that JS is getting symbols, things might change, but I expect
methods will still have plain old string names, just like always. The
class syntax was designed with this assumption.

>     var dict = Object.create(null);
>     Object.defineProperty(dict, 'iterator', { value: function*(){ /*...*/ }
> })

You can certainly still use an object as a worse-is-better dictionary;
you'd just leave the iterator code outside of the object:

    function* props(obj) {
        for (var k in obj)
            yield [k, obj[k]];
    }

    for (let [k, v] of props(dict))
        ...

This is probably what you'd want to do anyway. It's less code.

Since you want something iterable, it's also worth considering Map.
Iteration built in!

> It's `__proto__` all over again (looking at __proto__ as the "get/set [[Prototype]] protocol"). Just say no.

...Oh, that seems like a bit much. __proto__ was opt-out, for one
thing. Iterators are opt-in.

-j


More information about the es-discuss mailing list