closures of javascript
Feb 19, 2017
since javascript will bound variable scope to its declaration,
or says it will keep the local context.
let’s define a function that will return a function for step add.1234567function step_iterator(start, step) { return function() { var res = start; start = start + step; return res; }}
then we can get an iterator by call the function1var numberWithStep2 = step_iterator(0, 2);
and javascript will keep it as a function in numberWithStep2
each time we call the function numberWithStep2
123456numberWithStep2();// will return 0numberWithStep2();// will return 2numberWithStep2();// will return 4
pretty cool! huh?