Chapter 11. Serious functions

Anonymous Functions, Scope and Closures

image with no caption

You’ve put functions through their paces, but there’s more to learn. In this chapter we take it further; we get hard-core. We’re going to show you how to really handle functions. This won’t be a super long chapter, but it will be intense, and at the end you’re going to be more expressive with your JavaScript than you thought possible. You’re also going to be ready to take on a coworker’s code, or jump into an open source JavaScript library, because we’re going to cover some common coding idioms and conventions around functions. And if you’ve never heard of an anonymous function or a closure, boy are you in the right place.

Note

And if you have heard of a closure, but don’t quite know what it is, you’re even more in the right place!

Taking a look at the other side of functions...

image with no caption

You’ve already seen two sides of functions—you’ve seen the formal, declarative side of function declarations, and you’ve seen the looser, more expressive side of function expressions. Well, now it’s time to introduce you to another interesting side of functions: the anonymous side.

By anonymous we’re referring to functions that don’t have names. How can that happen? Well, when you define a function with a function ...

Get Head First JavaScript Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.