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

12

u/lostPixels May 28 '19

In my experience working on a codebase with 10,000+ LOC, we never use closures. Every method is generally treated as atomic, where every call just accepts arguments are predictably returns results. Closures are cool but in my opinion can be confusing enough that they will cause some less JS focused developers a lot of issues debugging.

9

u/theycallmeepoch May 28 '19

Yes, I feel like a pure functional programming approach helps minimize the need.

It seems like it was good for making your own getter and setter functions but those are built into JS now...

1

u/[deleted] May 31 '19

I feel exactly the opposite. In a purely functional codebase, you're almost definitely going to be using curried functions and partial application, which only exist in Javascript because of closure.

1

u/ScientificBeastMode strongly typed comments May 28 '19

I tend to agree with you there. Predictability is important for testing functions, and closures add complexity.

But as far as function dependencies go, closures provide a better way to inject dependencies than simply storing dependencies in some outer scope that other parts of your code can have access to and possibly mutate.

At least you know that the only functions that have access to that dependency are the ones that were returned from that closure. But it’s not something I would want to do very often.

1

u/lostPixels May 28 '19

I agree with you totally. These days, my team usually will be working with React and we stick with pure functional programming as much as possible. However in the past I have had times where a closure would work, but in that case I actually would opt to use the new keyword and made instances of a Thing that all have internal scope, but could be observed and interacted with. It's really just a question of preference and using the right tool for the job.

1

u/ScientificBeastMode strongly typed comments May 28 '19

That’s probably the better option, both for transparency and for memory safety. But I’m definitely on board the functional programming train nowadays, especially with React.

1

u/fallen_atreus May 28 '19

When we talk about atomic functions that produce always the same kind of output with the given output, are we not talking about the functional programming paradigm?

1

u/[deleted] May 31 '19

You don't use partial application or currying?

0

u/FormerGameDev May 28 '19

so, you never pass an anonymous function to array.map, filter, etc ?