r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

Show parent comments

15

u/Mydrax May 28 '19

Well, 2 really awesome use cases of closures I could name off the top of my head are:

  1. When you create function factories, where the outer function can be used to create functions that have similar concerns.

  2. The module pattern, that can be used to create private functions in javascript (where you return a set of functions whose logic is encapsulated).

2

u/turntopage294 May 28 '19

I'd been using 2 without knowing about closures, actually, for a time; I had an 'Oh, I see' moment after investigating when because closures love vaguely familiar

2

u/ScientificBeastMode strongly typed comments May 28 '19

Object factories are cool, and even class factories!

0

u/Memorytoco May 28 '19

just when use setTimeout and pass something like ( ) => {this.dosomething()} in some objects' method. or Array.forEach((entry) =>{ this.something()}).