I am trying to lear generators but before going into that I want to know how/why this works
function makeIterator(array){
var nextIndex = 0;
return {
next: function(){
return nextIndex++
}
}
}
var it = makeIterator([1,2,3,4]);
Why is it that it.next()
return 1
and another it.next()
returns 2
. How does it know?
Is there a way to see how it works behind the scenes?