Chapter 4. Wielding functions

 

This chapter covers

  • Why anonymous functions are so important
  • The ways that functions can be referenced for invocation, including recursively
  • Storing references to functions
  • Using the function context to get our way
  • Dealing with variable-length argument lists
  • Determining whether an object is a function

In the previous chapter, we focused on how JavaScript treats functions as first-class objects, and how that enables a functional programming style. In this chapter, we’ll expand on how to use those functions to solve various problems that we might come across when authoring web applications.

The examples in this chapter were purposefully chosen to expose secrets that will help you to truly understand JavaScript functions. Many are simple in nature, but they expose important concepts that will be broadly applicable to the dilemmas we’re bound to run into in future coding projects.

Without further ado, let’s take the functional JavaScript knowledge that we now possess in our two hands and wield it like the mighty weapon that it is.

4.1. Anonymous functions

You may or may not have been familiar with anonymous functions prior to their introduction in the previous chapter, but they’re a crucial concept we all need to be familiar with if we’re striving for JavaScript ninja-hood. They’re an important and logical feature for a language that takes a great deal of inspiration from functional languages such as Scheme.

4.2. Recursion

4.3. Fun with function as objects

4.4. Variable-length argument lists

4.5. Checking for functions

4.6. Summary

sitemap