So that were symbols,
now let's move on to iterations and generators and let's start with iterators. An iterator is simply
an object in the end which has a next method, which then in turn returns a result of a certain structure.
For that let's say we have another constant, company and that holds an object where we have employees
and there I got Max and I got Manu and I got Anna in there.
Now this is a regular object,
we make it iterable or we turn it into an iterator or you could say by adding a next method here
which we can add like this,
now the name has to be exactly next. Now in this next method here, we then should return an object where
we have a value property, this could be employees
this, employees zero
so the first element in the employees array and a done property which simply signals with a boolean
whether we got more values we can output or not.
Well then we should probably also have another property here, current employee which initially is zero which
we use to track which employee we already output
so that here we can access this current employee and store that in a returned value variable or constant, I'm
not going to change this
and thereafter we can set this current employee to current employee plus one by incrementing it and then
we return our value here. To be precise,
we also want to check if we still have employees left through which we can loop by checking if this current
employee is greater or equal than this employees length.
We know if this is the case, then we're out of employees because we then have no more employees left,
so here we could return a value of this current employee to return the number for which we failed and
set done to true to signal that we are done looping through that.
Now what's the idea behind this next method though?
Well now we can console log company next and execute this a couple of times down there.
If we save that and we reload,
you see we print all different employees until we're done.
So what we do here in the end is we create our own method that allows us to loop through a specific
field of this object and we could have any looping logic we want in here.
So this in the end kind of makes this a loopable object,
right now we can't use it with for/of but we soon will be able to do so but we have an object where
we have a method which we can call multiple times to go through any logic of our choice, in this case
to go through all employees but of course it's totally up to you through which field or whatever you want
to loop through here.
So we have a method which we can call multiple times and every time it will return something different
and not just that, it will actually go through some list of data, it will exhaust some list of data and
when we exhausted that list, we'll not get an error, we'll simply get a response, we'll get back a value
where we see that we're done because of this uniform return value where we always have a done property
which is true or false,
we also know if we can call next again and have more values left or if that's not the case.
Now this alone as I said might not be totally helpful yet but this already is a logic you could use
in some applications where you have an object which you use as a data storage and you want to go through
some custom logic there to output some data which is stored in there,
you don't want to use a for/in loop because you're maybe not interested in all the properties of this
object but just in some of the values in one specific property of this object and then such a custom
looping logic could be quite interesting here because what you could of course do with that is you could
create a while loop and have a result here or let's say an employee
and for that you call company next, then you have a while loop and as long as employee is not done, you
continue looping and then you can console log employee value here or do whatever you want to do with
it in the while loop and thereafter, set employee equal to company
next again
and now we have a dynamic loop, not a for/of loop but still a loop which will in the end go through
all employees with our own iterator logic.
So if we save that and we reload, it outputs the three employees we have and then it stops because then
done is true and therefore this exit condition kicks in or this running condition is no longer true
and therefore we make it out of that loop.
So this would be one example how you can use your own iterator, company, which has this next method which
then in turn allows you to have your own looping logic for whichever field or whichever logic related
to this company object you might have.