×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Nilesh Yadav
Added: Jul 17, 2017 5:51 AM
Views: 4
Tags: no tags
  1. let iterable = [1,2,3];
  2.  
  3. function createIterator(array){
  4.     let count = 0;
  5.     return{
  6.         next: function(){
  7.             return count < array.length?
  8.             {value: array[count++], done:false}:
  9.             {value: undefined, done:true};
  10.         }
  11.     }
  12. }
  13.  
  14. let myIterator = createIterator(iterable);
  15.  
  16. console.log(myIterator.next());
  17. console.log(myIterator.next());
  18. console.log(myIterator.next());
  19. console.log(myIterator.next());
  20.  
  21.  
  22.  
  23. /*Iterable {
  24.     [symbol.Iterator]() : Iterator
  25. }
  26. Iterator{
  27.     next() : IResultObj
  28. }
  29. IResultObj{
  30.     value: any
  31.     done: bool
  32. }
  33. */