Code smell? Iterator prototype has iterator method that returns "this"

Jordan Harband ljharb at gmail.com
Wed Jul 27 05:16:56 UTC 2016


`new weirdFunction()` would `=== iter` because a constructor that returns
an object (`this`), returns that object when `new`ed - which is how
functions have worked since ES3 (or probably ES1).

On Tue, Jul 26, 2016 at 9:31 PM, Michael Theriot <
michael.lee.theriot at gmail.com> wrote:

> There are many things I still don't understand about iterators...
>
> ```js
> var iter = [].values();
> iter[Symbol.iterator]() === iter;
>
> var weirdFunction = iter[Symbol.iterator];
> weirdFunction.call(iter) === iter;
>
> var weirdInstance = new weirdFunction(); // what is this??
> ```
>
>
> On Mon, Jul 25, 2016 at 10:19 AM, John Lenz <concavelenz at gmail.com> wrote:
>
>> This seems ripe for misuse:  you don't want two "owners" for the same
>> iterator calling "next" and normally, without the Iterator interface
>> implementation, you would expect "iterator()" to always return an instance
>> of the iterator that the caller "owned".
>>
>> Can anyone provide any historical context on why this method was added to
>> the "iterator"?
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160726/40f23f7e/attachment-0001.html>


More information about the es-discuss mailing list