JavaScript Functions Part 3

You can return functions from functions. Part 3 discusses that. In Part 2 we discussed how to pass functions to functions and before that, in Part 2 we discussed assigning functions to variables. This post is based on the book Head First JavaScript Programming Chapter 10: Liberated Functions, starting on page 450.

Let’s extend the airline example a bit and explore why and where we might want to return a function from a function. To do that, we’ll add another property to each of our passengers, the ticket property, which is set to “coach” or “firstclass” depending on the type of ticket the passenger has purchased:

var passengers = [ { name: "Jane Doloop", paid: true, ticket: "coach" },
	           { name: "Dr. Evel", paid: true, ticket: "firstclass" },
                   { name: "Sue Property", paid: false, ticket: "firstclass" },
                   { name: "John Funcall", paid: true, ticket: "coach" } ];

In first class you’re able to order a cocktail or wine, while in coach you’re more likely to be offered a cola or water.

Series Navigation<< JavaScript Functions Part 2

Leave a comment

Your email address will not be published. Required fields are marked *