o_O
Most people don't think of a function with an inherited scope as a closure since by the standard PLT people will tell you that Javascript does not provide "real" closures.
I'm sure you know what I meant. It's the "Javascript Way" TM of doing this.
My "little known" I mean that most programmers do not think of doing this out right. It took me seeing an amazing talk by the creator of JSON called "Javascript the Good Parts" to realize how powerful this construct was, and how Javascript made it so easy.
The point is that ES2015 defines clear interface for iterating over iterables. With help of Symbol and *.
If you would use this code in production, probably you would document it: ... returns iterator function.
next = fib()
next() // 0
next() // 1
next() // 1 ...
But if you would use Symbol.iteratorthen you can do
for (f of fib())
console.log(f)
or use gathering, spreading etc..from a given for loop:
for (x in function())
Please tell me when this for loop will end. You cannot.If you instead did...
next = fib()
while (true) {
number = next();
}
You are instantly presented for every case in which this loop will terminate.Now don't get me wrong, I DO fully agree that a generator is the correct method for doing this, I'm just pointing out another construct to allow you to do this.
Edit: Altogether, good show. I enjoyed the post.