If you purchased this ebook directly from oreilly.com, you have the following benefits:
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Foreword by Jeremy Ashkenas
Jeremy Ashkenas
This is a terribly exciting book.
Despite its ignominious origins as a Java-lite scripting language, intended to be embedded inline in HTML documents to allow a minimum modicum of interactivity JavaScript has always been one of the most essentially flexible languages for general purpose programming.
You can sketch, smudge, and draft bits of code in JavaScript, while pushing and twisting the language in the direction that best suits your particular style. The reason that this is more natural in JavaScript than in other, more rigid languages is due to the small set of strong core ideas that lie at the heart of JavaScript: Everything is an object (everything is a value) to an even greater extent than in famously object-oriented languages like Ruby and Java. Functions are objects, are values. An object may serve as prototype (default values) for any other object. There is only one kind of function, and depending on how you employ it, it can either serve as a pure function, a mutating procedure, or as a method on an object.
JavaScript enables, but does not enforce, many different programming styles. In the early days, we tended to bring our traditional expectations and best practices with us when we started to learn to write JavaScript. Naturally this led to much JavaScript resembling Java without the omnipresent types or even with the types still there, just living inside of annotation comments above each method. Gradually, experiments were made: folks started generating functions at runtime, working with immutable data structures, creating different patterns for object-orientation, discovering the magic of chaining APIs, or extending built-in prototypes with custom functionality.
One of my favorite recent developments is the enthusiastic embrace of functional programming ideas as appropriate tools for building rich JavaScript applications. As we move beyond form validation and DOM animation towards full-featured apps, where the JavaScript in your codebase might be getting up to any manner of hijinks in any particular problem space, functional ideas are similarly moving beyond the basic callback, and towards more interesting arenas, such as:
Building out a large API by partially applying a core set of functions with arguments in different configurations.
Using recursive functions to smooth the gap between actions that need to occur for a period of time, and events coming in rapid-fire off the event loop.
Structuring a piece of complex business logic as a pipeline of mutation-free changes that can later be plugged-into and pulled apart.
Youre reading the ideal book with which to explore this territory. In the following nine chapters (and two appendixes), your friendly tour guide and resident mad scientist, Michael Fogus, breaks down functional programming into its basic atoms, and builds it back up again into edifices of terrifying cleverness that will leave you wondering. Its rare that a programming book can take you by surprise, but this one will.
Enjoy.
Foreword by Steve Vinoski
Steve Vinoski
I remember when I first read Douglas Crockfords wonderful book JavaScript: The Good Parts . Not only did I learn from it, but the fact that Crockford required only 172 pages to steer readers away from JavaScripts problematic parts makes his work that much more impressive. Brevity is often at odds with educative exposition, but when an author achieves both as Crockford did, the reader is more likely to fully digest the authors recommendations and benefit from them.
In the pages that follow, youll find that Michael Fogus has given us a book as excellent as Crockfords, perhaps more so. Hes built on the sound advice of Crockford and other predecessors to take us on a deep dive into the world of functional JavaScript programming. Ive often heard and read (and even written myself) that JavaScript is a functional programming language, but such assertions (including my own) have always seemed light on the pragmatic details that practicing programmers need. Even Crockford devoted only a single chapter to functions, focusing instead, like many authors, on JavaScripts object support. Here, merely saying that Fogus fills in those missing details would be a serious understatement.
Functional programming has been a part of the computing field from its inception, yet traditionally it has not enjoyed significant interest or growth among practicing software professionals. But thanks to continuing advances in computing hardware speed and capacity, coupled with our industrys increasing interest in creating software systems of ever-escalating levels of concurrency, distribution and scale, functional programming is rapidly growing in popularity. This growth is due to the observation that functonal programming appears to help developers reason about, build and maintain such systems. Curiosity about languages that support functional programming, like Scala, Clojure, Erlang and Haskell, is at an all-time high and still increasing, with no abatement in sight.
As you read through Michaels insightful investigations of JavaScripts functional programming capabilities, youll be impressed with the significant depth and breadth of the information he provides. He keeps things simple at first, explaining how functions and data as abstraction can avoid the desire to use JavaScripts powerful object prototype system to create yet another way of modeling classes. But as he explains and thoroughly reveals in subsequent chapters, the simple model of functional data transformation can yield sophisticated yet efficient building blocks and higher level abstractions. I predict youll be amazed at just how far Fogus is able to take these innovative approaches as each chapter goes by.
Most software development efforts require pragmatism, though, and fortunately for us Fogus tackles this important requirement as well. Having beautiful, sophisticated and simple code is ultimately meaningless if its not practical, and this is a large part of the reason functional programming stayed hidden in the shadows for so many years. Fogus addresses this issue by helping the reader explore and evaluate the computing costs associated with the functional programming approaches he champions here.
And of course books, just like software, are ultimately about communication. Like Crockford, Fogus writes in a manner thats both brief and informative, saying just enough to drive his ideas home without belaboring them. I cant overstate the importance of Michaels brevity and clarity, since without them wed miss the incredible potential of the ideas and insights hes provided here. Youll find elegance not only in the approaches and code Fogus presents, but also in the way he presents them.