|
| 1 | + |
| 2 | +##Iterable and Iterator Protocols |
| 3 | + |
| 4 | +As ES6 introduced new syntax, it also introduced new protocols, these protocols can be implemented by any object respecting some conventions. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +##Iterable protocol: |
| 9 | + |
| 10 | +iterable protocol allows javascript objects to define or customize their iteration behavior such as what values are looped over in a`for .. of` construct. |
| 11 | + |
| 12 | +###Iterable Built-in types: |
| 13 | + |
| 14 | +some types has built-in interation behavior, this means that you can directly contrcut`for .. of` on these types example: |
| 15 | + |
| 16 | +1. Array |
| 17 | +2. Map |
| 18 | +3. String |
| 19 | + |
| 20 | +####Example 14.0: |
| 21 | + |
| 22 | +```javascript |
| 23 | +constmyArray= [1,2,3]; |
| 24 | +for(constnumof myArray) { |
| 25 | +console.log(num); |
| 26 | +} |
| 27 | + |
| 28 | +// 1 |
| 29 | +// 2 |
| 30 | +// 3 |
| 31 | + |
| 32 | +``` |
| 33 | + |
| 34 | +As we can see in this example myArray (of type`Array`) has a built-in iteration behavior |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +###Non-iterable type: |
| 39 | + |
| 40 | +An example of non iterable type is`Object`, but we can make an`Object` iterable by implementing`@@iterator` method, this means that the Object or its prototype needs to have a property with key`@@iterator` this is available via a constant`Symbol.iterator` |
| 41 | + |
| 42 | + |
| 43 | +`Symbol.iterator` is a zero argument function that returns an object, conforming to the iterator protocol |
| 44 | + |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | + |
| 49 | +##Iterator protocol: |
| 50 | + |
| 51 | +Iterator protocol defines a standard way to produce a sequence of values, an object is iterator when it implements a`next()` method with the following semantics |
| 52 | + |
| 53 | +1. 0 argument function`next()` that returns an object |
| 54 | +2. The returned object should contain 2 properties`done` (Boolean) and`value` (Any javascript type value) |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | + |
| 59 | +##How to make an iterable object |
| 60 | + |
| 61 | +As we discussed for an object to be iterable it has to contain a property with key`Symbol.iterator` that contains a 0 argument function, where that function should return an iterator Object that contains`next()` method, where that method returns an Object that contains`value` and`done` properties:D !!! |
| 62 | + |
| 63 | + |
| 64 | +####Example 14.1: |
| 65 | + |
| 66 | +```javascript |
| 67 | +let numbersIterable= {}; |
| 68 | +// After implementing `Symbol.iterator` numbersIterable became iterable object |
| 69 | +numbersIterable[Symbol.iterator]=function() { |
| 70 | +let number=0; |
| 71 | +// This returned object is called iterator object |
| 72 | +return { |
| 73 | +next:function() { |
| 74 | +if(number<10) { |
| 75 | +return { done:false, value:++number }; |
| 76 | + }else { |
| 77 | +return { done:true }; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +// for..of is introduced in ES6 and it is a consumer to iterables, you can't use for..of on non iterable objects |
| 83 | +// Since we change `numberIterable` to be iterable now we can use for..of |
| 84 | +for(let numof numbersIterable) { |
| 85 | +console.log(num); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +##How to make an iterator(not iterable) object |
| 92 | + |
| 93 | +As we can see from example 14.1 we had to create iterator object to make`numberIterable` iterable, it is very important to differentiate between`iterable` and`iterator`, in the next example we will create iterator |
| 94 | + |
| 95 | +####Example 14.2: |
| 96 | + |
| 97 | +```javascript |
| 98 | +letnumbersIterator=function(max) { |
| 99 | +let number=0; |
| 100 | +return { |
| 101 | +next:function() { |
| 102 | +if(number< max) { |
| 103 | +return { done:false, value:++number }; |
| 104 | + }else { |
| 105 | +return { done:true }; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +constmyNumbersIterator=numbersIterator(6); |
| 111 | +console.log(myNumbersIterator.next().value);// 1 |
| 112 | +console.log(myNumbersIterator.next().value);// 2 |
| 113 | +console.log(myNumbersIterator.next().value);// 3 |
| 114 | +console.log(myNumbersIterator.next().value);// 4 |
| 115 | +console.log(myNumbersIterator.next().value);// 5 |
| 116 | +console.log(myNumbersIterator.next().value);// 6 |
| 117 | +console.log(myNumbersIterator.next().value);// undefined |
| 118 | +``` |
| 119 | +--- |
| 120 | + |
| 121 | +##Javascript iterable based constructs: |
| 122 | + |
| 123 | +1.`for-of` loop |
| 124 | +2.`[]` array destrcuturing pattern |
| 125 | +3.`...` spread operator |
| 126 | + |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | + |