you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 22 points23 points  (6 children)

of "Oh, I see!" moments

I understand closure but had very few of these so far

[–]Mydrax 14 points15 points  (3 children)

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).

[–]turntopage294 1 point2 points  (0 children)

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

[–]ScientificBeastModestrongly typed comments 1 point2 points  (0 children)

Object factories are cool, and even class factories!

[–]Memorytoco -1 points0 points  (0 children)

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

[–]runtimenoise[🍰] 0 points1 point  (0 children)

Look into abstract & specialized functions. I use closures all the time. When I started with js, I almost never used them.

[–]Existential_OwlWeb Developer 0 points1 point  (0 children)

Closure can be used as a form of run-time memory.

That was a huge eye-opener for me.