The closure feature is widely used in JavaScript and make it quite difficult for programmer who was used to program in a no-closure language. So what is the best practice about using closure and how to avoid shooting yourself in the foot.
|
|
In my opinion, the best way to avoid shooting yourself in the foot with a cool language feature is don't use it if you don't need it. Find out what closures can do for you and how they are commonly used and stick with standard patterns unless you really need (not want) to use them for something else. If you just want to use them (for instance, to learn them), do it on your own play project. |
||||
|
|
|
Understand what a closure is
Understand how the closure will grow
Watch out for capturing references
Don't close over variables you don't need to close over
Make the functions that will become closures as simple as possible
Don't create closures when they aren't needed
|
|||
|
|
|
In general, you can avoid shooting yourself in the foot by:
Play around with closures, figure out how they work, then you can use them confidently and get expected results. |
|||
|
|
