- [Instructor] In the last lesson we introduced the iterator
pattern and modified our code to use the pattern.
In this lesson, we're going to build the iterator class and
make our application work.
So inside of the exercise files for Chapter Four,
Chapter Four Lesson 7 and within the start folder
I'm going to go ahead and create a new file
that we're going to call iterator.js.
And for the iterator, I'm just going to create a class
called Iterator and I'm going to make sure I export that
class and I will come up within the class and then give it
a constructor and when we create a new instance of the
iterator, we need to collect an array.
I'm just going to call those Items and we will go ahead and
add that to our constructor arguments.
We'll also default it as an empty array,
so if we accidentally do not provide anything,
we will have an empty iterator.
Now, the methods that we need to add to this are first,
last,
next
and prev.
So the methods that we use for iterating to an object.
It's also a good idea with iterators to implement a hasNext
or a function that will let us know if there's another
record or if we're on the last one.
And also something that will give us the current.
And in case we don't want the next or the previous item
but we just want to take a look at the current item again,
we can access it through the current method.
So what we need to do is handle the mechanics of the array
within this class,
which means we need to save an index.
So I'm going to set the index to zero for the first item
within the array and then I'm also going to save the item
after this dot items.
So when we create a new iterator, the index to start off
is zero and this dot items represents the items.
So if we want to get the current item and being that our
current index is zero,
all we have to do is come within this current method
and return this dot items this dot index.
If we want to get the first item
all we have to do is return this dot items zero.
Now, we could return this dot items zero but JavaScript
actually provides us with a pretty cool way of doing this.
So I can actually destructure the first item from an array
simply by surrounding that variable in brackets and now I
can save this dot items and it will always give me the
first item.
So I'll go ahead and put that out there
instead of using the zero.
And the reason that I want to do that...
So in order to get the last item I'm going to do
something similar.
I'm going to destructure the first item from an array
but the array that I'm going to destructure the first item
from is going to be this dot items dot reverse.
Now, the problem with reverse is
is if I actually run reverse on this dot items,
it will change their order.
So I'm going to actually quickly use the spread operator to
create a copy of this array.
So now I'm creating a copy of the items array,
reversing it and then grabbing the first item
from that array.
That is actually our last item.
So if we grab the first item from the reverse array,
that's going to be our last item.
So I'm going to go ahead and change that to last
and then return the last item.
Now let's check to see if we have a next item based upon the
location of our current index.
So I'm going to return this dot index less than this dot items
dot length minus one.
And now our hasNext will return a true if there is a next
record, or a false if we're currently on the last record
in the array.
Finally, we can come down here and code our next.
So what we're going to do is make sure that we have a next.
So if this dot hasNext exists
then we will go ahead and say this dot index,
we can increment it by one.
And then I'm going to return this dot current.
So we're going to increment the index by one and then return
the current item based upon that index.
That will be our next record.
And then previous is going to be just
a reverse of the same thing,
so we're going to check the current index
if this dot index
is not equal to zero
then we're just going to decrement the index.
This dot index minus equals one and then we will return
this dot current.
So there is our iterator.
This class handles all of the mechanics for dealing with
the array and now all we have to do is send an array to
this class and we can access the next record, the previous
record, the current record, the first record, the last
record and we can also check to see if it even has a
next record.
Let's go ahead and give this a shot.
Let me come over to the terminal and now I will type
node index dot js and it looks like I need to
spell require correctly.
So let me come in here
into index file and where I include the iterator I forgot
to spell require.
So let's go ahead and try this again.
I'll clear that message, node index dot js.
Now with the application running we can see that pressing
the right arrow will iterate forward to the array.
Pressing the back arrow will iterate backwards to the array.
And then pressing the up arrow will take us to the
first record and pressing the down arrow will take us to
the last record.
So that's the point of an iterator.
I can now use this iterator class to iterate to any array
without worrying about the mechanics of
dealing with that array.