Chapter 5. Closing in on closures

 

In this chapter we discuss

  • What closures are and how they work
  • Using closures to simplify development
  • Improving performance using closures
  • Solving common scoping issues with closures

Closely tied to the functions that we learned all about in the previous chapters, closures are a defining feature of JavaScript. While scores of page authors get along writing on-page script without understanding the benefits of closures, the use of closures can not only help us reduce the amount and complexity of the script necessary to add advanced features to our pages, they allow us to do things that would simply not be possible, or would simply be too complex to be feasible, without them. The landscape of the language, and how we write our code using it, is forever shaped by the inclusion of closures.

Traditionally, closures have been a feature of purely functional programming languages. Having them cross over into mainstream development has been particularly encouraging, and it’s not uncommon to find closures permeating JavaScript libraries, along with other advanced code bases, due to their ability to drastically simplify complex operations.

In this chapter, we’ll explore what closures are all about and look at how we can use them to elevate our on-page script to world-class levels.

5.1. How closures work

5.2. Putting closures to work

5.3. Binding function contexts

5.4. Partially applying functions

5.5. Overriding function behavior

5.6. Immediate functions

5.7. Summary

sitemap