Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> what are generators and the yield keyword for?

Generators are great for memory intensive data structures (ie. large lists), as they provide lazy evaluation to languages that are designed to evaluate eagerly. Generators can be considered to be a first step towards coroutines if you will, as the subroutines are the ones yield-ing control back to the event loop - David Beazley has a great talk[0] on it, coding an event loop live from scratch.

> similar but subtly different things like for/of vs for/in vs forEach vs iterating Object.keys(), Object.values(), Object.entries()

Array.forEach[1] is the oldest of that bunch, Object.keys[2] came later, for...in[3]/for...of[4] after that, and Object.values[5]/Object.entries[6] are the newest addition. I personally prefer the new way here.

EDIT: for...in[3], then Array.forEach[1], then Object.keys[2], then the rest it seems.

[0]: https://youtu.be/MCs5OvhV9S4?t=1072 (video)

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[4]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[5]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[6]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: