Introduction - CoffeeScript in Action (2014)

CoffeeScript in Action (2014)

Introduction

I’ve long thought that the things that will ultimately resonate the most with people don’t reveal themselves immediately. Instead, they might initially present themselves as slightly interesting but not striking. I’ve seen this with music, film, literature, and every other aspect of human experience that I’ve looked at for any substantial amount of time. I’ve also seen this with at least two programming languages, JavaScript and CoffeeScript.

My early reaction to JavaScript was dismissive. Strangely, or not, years later I would be working almost exclusively in it. My early reaction to CoffeeScript was also dismissive. “Here we go,” I thought. “Yet another tool created because people don’t understand JavaScript!” I was wrong about CoffeeScript just like I was wrong about JavaScript.

CoffeeScript is not about avoiding JavaScript—it is about understanding JavaScript. This applies to both people who are already familiar with JavaScript and people who are not familiar with JavaScript. Learning CoffeeScript helps people to understand JavaScript. At the same time, for many people it makes writing JavaScript programs simpler and more enjoyable, which means that it makes sense for them to use CoffeeScript instead of JavaScript.

About this Book

This is a language book. It doesn’t try to comprehensively detail libraries, frameworks, or other ancillary matters. Instead, it concentrates only on teaching the CoffeeScript programming language from syntax, through composition, to building, testing, and deploying applications. Although this book is full of complete, working programs, they’re all manufactured (contrived, if you will) slaves to the core goal of helping you learn to program in CoffeeScript. You’ll find this book to be a useful reference because of its breadth and depth, but it isn’t comprehensive. The web made comprehensive programming references obsolete long ago.

If you want to learn the CoffeeScript language, then this book is for you. If, instead, you want to eschew that learning in favor of ready-made instructions for using CoffeeScript with one framework or another, then this is probably not the book for you. Although references to popular frameworks are given, this book concentrates on CoffeeScript as a language. This book balances server-side and client-side uses of CoffeeScript as appropriate to each individual topic.

Roadmap

This book follows a three-act structure in which you, the hero, journey to the heart of CoffeeScript before emerging with a thorough grasp of it.

Part 1 sets up your core understanding of the language. When you begin your apprenticeship in chapter 1, you’ll learn the motivations for creating CoffeeScript and why you are embarking on your journey. In chapter 2 you’ll be immersed in the syntax of the language and begin to absorb it. Inchapter 3 you’ll learn about functions from the ground up, and in chapter 4 you’ll do the same with objects.

In part 2 you’ll learn how to wield your new understanding of CoffeeScript. Chapter 5 will have you pulling apart objects and putting them together, and chapter 6 will have you creating functions from functions, and from functions that create functions. In chapter 7 you’ll hone your craft, technique, and style. After that comes chapter 8, which leads you right to the heart of CoffeeScript where you’ll learn to change the language itself. Finally prepared, in chapter 9 you’ll enter the dragon’s lair of asynchronous programs.

In part 3 your travels will take you further from home where you’ll learn how to build entire applications. This starts in chapter 10 where you’ll learn about test-driven development. In chapter 11 you’ll learn about building user interfaces for web browsers. In chapter 12 you’ll wrap everything up by building and packaging applications, ready for the world to see. Finally, chapter 13 looks at the future and where you, the journeyman, are headed with CoffeeScript.

Prerequisites

This book doesn’t assume any knowledge of CoffeeScript. Although some familiarity with JavaScript will make things easier, no level of JavaScript experience is assumed. What is assumed is some experience with programming (any language will do) and a basic grasp of web development concepts. Finally, although the Node.js platform is used throughout the book, no prior knowledge of Node.js is assumed.

Code conventions

Any source code listings inline within the text, such as read 'book', are formatted using a fixed-width font. Blocks of code are also formatted in a fixed-width font, separated from page content:

read = (material) ->

console.log "Reading #{material}"

read 'CoffeeScript in Action'

Within blocks of code, a # character at the start of a line indicates that what follows the # is the result of evaluating the immediately preceding line:

read = (material) ->

console.log "Reading #{material}"

read 'CoffeeScript in Action'

# Reading CoffeeScript in Action

In this way, all of the code snippets can be pasted directly into a CoffeeScript prompt, where the output you see should match the comment. A similar approach is taken with JavaScript code snippets where # is replaced with //:

'Raw' + ' JavaScript'

// 'Raw JavaScript'

Formatted code snippets can be copied and pasted from HTML files, one for each chapter, which are available for download from the publisher’s website and also from links inside eBook versions of CoffeeScript in Action.

Invoking programs on the command line is shown by prefixing each line with a >. Expected output is prefixed with a #:

> coffee -e "console.log(3 + 4);"

# 7

Being prefixed with a >, the command-line examples have the disadvantage that they can’t be pasted directly into a prompt. Occasionally, to provide a clear distinction between general command lines and the prompt for a particular program, the listing is prefixed with the name of the program prompt followed by a >:

node>

Before you can run any of this code, you need to have CoffeeScript installed.

Installing CoffeeScript

This book assumes that you have Node.js installed. To install Node.js, visit the website at http://nodejs.org and follow the instructions for your system. Once you have Node.js installed, you’ll be able to run Node.js from the command line:

> node

This will land you in the Node.js prompt, into which you can enter raw JavaScript:

node> 1 + 2;

// 3

To exit, enter Ctrl-C twice:

node> <CTRL-C>

// (^C again to quit)

node> <CTRL-C>

>

Installing Node.js also installs npm (Node packaged modules), which you’ll use for installing packages. Use npm to install CoffeeScript for all users on your system:

> npm install –g coffee-script

You now have CoffeeScript installed. Enter coffee into your command line:

> coffee

This launches you into a CoffeeScript prompt that will be your constant companion throughout this book. The command has other functionality besides the prompt, which you can see via --help:

> coffee --help

This will list the options. The meaning of particular options is given later in this book where needed, but not all options are covered.

As with Node.js, to exit this prompt, enter Ctrl-C twice.

Code downloads

All of the book’s listings are available in the downloadable code from the publisher’s website at www.manning.com/CoffeeScriptinAction and also on GitHub at https://github.com/boundvariable/coffeescript-in-action. When you obtain the downloadable code, go to the directory containing it and run the command npm install. Suppose you have the downloadable code at ~/src/coffeescript-in-action:

> cd ~/src/coffeescript-in-action

> npm install

You’ll see npm install some packages that you’ll need. Once that’s done, you’re ready to start running the listings.

The downloadable code is organized into directories corresponding to chapter numbers, with each chapter folder containing a “listings” folder with files named to correspond to listing numbers for that chapter. Where possible, code listings are standalone programs, and you can run them by passing them to the coffee command. Suppose you wanted to run the imaginary listing 77 from chapter 77:

> coffee 77/listings/77.coffee

Some code listings in the book are complete programs that aren’t made to run on the command line but require a browser to run. In those cases, a command-line server program that will enable you to run them is provided either as one of the listings or along with the listings. Wherever that is the case, instructions are provided for running the server.

Exercises

Throughout the book you’ll find some recommended exercises designed to help you better understand the concepts presented. The exercises range from small and closely defined to more open-ended exercises intended for exploration.