The better you understand it the more efficient your architecture becomes and less time you spend debugging issues that involve global and local variables not being available to functions etc.
I would really love to see some examples of this as it's not clear to me yet the benefits other than creating a nested function which has "private" variables. I'm not quite at Closures yet in "Secrets of the Javascript Ninja".
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.
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.
18
u/[deleted] May 28 '19
The better you understand it the more efficient your architecture becomes and less time you spend debugging issues that involve global and local variables not being available to functions etc.