by SeanLuke · 7y ago · 0 comments
function getGetNext() {
let i = 2;
return function getNext() {
const next = i;
i = i * 2;
return next;
}
}
As a lisp coder [and not a JS coder], I'm wondering: why does the function getNext() have to be named? Why not just say something like: function getGetNext() {
let i = 2;
return function() {
const next = i;
i = i * 2;
return next;
}
} return () => {
const next = i;
i = i * 2;
return next;
}
But in this case the two are equivalent. function * getNext(){
let i = 2;
while(true){
i = i * 2;
yield i;
}
}
Here, no closure needed.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...