Page 148 - JavaScript
P. 148

var beforePrevious = 1;
             while(true) {
                 var current = previous + beforePrevious;
                 beforePrevious = previous;
                 previous = current;
                 yield current;  //This is like return but
                                 //keeps the current state of the function
                                 // i.e it remembers its place between calls
             }
         }

         var fib = FibonacciGenerator();
         fib.next().value; //2
         fib.next().value; //3
         fib.next().value; //5
         fib.next().done; //false


        Read Behavioral Design Patterns online: https://riptutorial.com/javascript/topic/5650/behavioral-
        design-patterns

































































        https://riptutorial.com/                                                                             105
   143   144   145   146   147   148   149   150   151   152   153