December 2017
Beginner
372 pages
10h 32m
English
Let's create a function that could be used as a counter; the custom logic we'd have for our counter will be that after the counter reaches 100, it should reset to 0 and then continue again.
To implement this, we will create a customCounter class which will implement the Iterator interface. In the next method, we will implement our custom logic. Let's look at the following code and then discuss how to implement the Iterator interface:
class customCounter implements Iterator<number>{ private calculatedVal:number=0; next(value?: any): IteratorResult<number>{ this.calculatedVal = this.calculatedVal > 99 ? 0 : ++this.calculatedVal; return{ done: false, value: this.calculatedVal } }}let c = new customCounter(); ...
Read now
Unlock full access