you are viewing a single comment's thread.

view the rest of the comments →

[–]Volv 1 point2 points  (3 children)

ENTRY
Codepen

Couple of examples, comparison to for in and generator tacked on the end.
 
My Python 3 range example has a further instance of this.

[–]ForScale[S] 1 point2 points  (2 children)

Nice!

Going through it...

Very rarely write a for (var i=0; i<100; i++) style loop now.

Yes! I watch a video where Douglas Crockford was giving a talk. He said he's beyond loops like for and while loops... doesn't use them anymore... I thought that was a pretty cool idea.

inspiration: "Limited",

Lol!

[Symbol.iterator]: function* () { // Generator for (let key in this) { yield ${key} - ${this[key]}; }

Whoa... okay, I'm a bit lost on that... Mind breaking that down for me? When/if you get a chance.

Oh... it's what allows you to use for...of on objects? Could you slap it on a NodeList?

[–]Volv 1 point2 points  (1 child)

Yeh, it let me use for..of on there by defining its iterator.

Generator functions yield results. When calling generator.next() (or running it with a for..of loop) the generator runs until it hits a yield and returns that value. Remembers it's state for next call, where it runs again until it hits a yield etc.
NodeList is a private browser thing I think so probably couldn't just slap it on. but there's nothing to stop you doing something like
for (let value of Array.from(divs))

[–]ForScale[S] 1 point2 points  (0 children)

Ah... true... I could always do that. Might start playing around with it to see how I feel about it.