Part 2. Functions

Part 2 takes the objects defined in the first part and brings them to life. Functions kick the gears into motion on any JavaScript application. Unfortunately, most JavaScript developers don’t take full advantage of the power of JavaScript functions. By taking advantage of the fact that functions are also objects in the system, you’ll begin to appreciate the joy of JavaScript. Part 2 also introduces new syntax that will change how you structure your JavaScript code: the pipeline and bind operators.

Chapter 4 starts by teaching you how to use JavaScript in a functional manner. You’ll learn to decompose problems into small tasks, each represented by a function, and to compose them back together. To enable this capability, higher-order functions allow you to pass, return, and dynamically call functions. You’ll learn how to use partially applied (curried) functions to prebake or configure the functions you’ll assemble into compositional chains. Also in store in chapter 4 is a preview of JavaScript’s proposed pipeline operator, which will bring the power of functional languages like Elixir and F# to JavaScript.

I’ve said that functions are objects, but chapter 5 reverses this definition, teaching you to think of objects as behaving like functions. Here, you’ll learn about a pattern that’s becoming pervasive on many language platforms: Algebraic Data Type (ADT). An ADT is an object with simple, specialized behavior. You can use ADTs to represent common tasks (data validation, error handling, null checks, and so on) in a compositional, fluent way. You’ll be able to execute objects as easily as you call functions—a concept called a functor. In fact, you’ve been using functors all along without realizing it. Arrays and promises (discussed in part 4) are perhaps the most commonly used functors in JavaScript, and you’ll learn which part of their respective APIs makes them behave like functors and even monads. What’s cool is that you’ll be able to extract this API and apply JavaScript’s proposed bind operator syntax to any type of object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset