Basket class in ES6.
Now that we have a complte ES6 classes with Iterator/Generator in it, lets optimize our Iterator method a little bit.
Reusing Array’s Iterator
If you notice, the
Basket class uses an Array type
container to hold the items. As Array type has a built-in Iterator, we can use that Iterator instead of writing our own in our classes (thanks to this tip). With this, we can rewrite our Iterator method like this:
Notice that our Iterator is no longer a generator function (lack of
* syntax), because it need not be.