Book Image

CoffeeScript Application Development

By : Ian Greenleaf Young
Book Image

CoffeeScript Application Development

By: Ian Greenleaf Young

Overview of this book

JavaScript is becoming one of the key languages in web development. It is now more important than ever across a growing list of platforms. CoffeeScript puts the fun back into JavaScript programming with elegant syntax and powerful features. CoffeeScript Application Development will give you an in-depth look at the CoffeeScript language, all while building a working web application. Along the way, you'll see all the great features CoffeeScript has to offer, and learn how to use them to deal with real problems like sprawling codebases, incomplete data, and asynchronous web requests. Through the course of this book you will learn the CoffeeScript syntax and see it demonstrated with simple examples. As you go, you'll put your new skills into practice by building a web application, piece by piece. You'll start with standard language features such as loops, functions, and string manipulation. Then, we'll delve into advanced features like classes and inheritance. Learn advanced idioms to deal with common occurrences like external web requests, and hone your technique for development tasks like debugging and refactoring. CoffeeScript Application Development will teach you not only how to write CoffeeScript, but also how to build solid applications that run smoothly and are a pleasure to maintain.
Table of Contents (19 chapters)
CoffeeScript Application Development
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

If you do web development, chances are you've at least heard of CoffeeScript. Though it's less than five years old, this little language has received a lot of attention, and it's getting harder to ignore. Maybe you've already worked with it a little bit, or maybe you're just wondering what the fuss is all about. Good news! CoffeeScript is a delightful language that can help you write better code and have fun doing it. In this book, we will explore the language itself, and find out first-hand how it can help us build beautiful web applications.

What is CoffeeScript?

CoffeeScript is a programming language. Like most programming languages, it offers control structures to describe the logic of our application, simple data types to store and manipulate information, and functions to encapsulate sections of program execution.

What makes CoffeeScript special is the way it is compiled. When most languages are compiled, they are translated into machine code—low-level instructions to the computer's processor. CoffeeScript is different: when compiled, it is instead translated into JavaScript. We write CoffeeScript code, give it to the CoffeeScript compiler and receive JavaScript code as output. This output can then be passed to anything that consumes JavaScript, such as a browser, or a standalone JavaScript interpreter.

This technique, dubbed transcompilation, allows us to use an alternative language on platforms that only directly support JavaScript. Client-side web development is the most prominent example, since JavaScript is the only supported general-purpose scripting solution on most web browsers. Other platforms such as Node.js and Rhino also offer useful features, but expect JavaScript input. JavaScript is nothing if not prolific, and CoffeeScript allows us to make use of all that existing tooling, but to write our code in a different language.

Why CoffeeScript?

CoffeeScript was certainly not the first (or last) language to target JavaScript platforms. Many established languages, such as Ruby, Python, C, and Java have one or more projects focused on compiling that language to JavaScript. And other languages have been developed specifically to target JavaScript—notably Dart, TypeScript, and Coco.

Note

The CoffeeScript wiki itself maintains an extensive list of other languages that compile to JavaScript. You can find it at https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS.

While it's not alone in its approach, CoffeeScript has seen the most success of any language that compiles to JavaScript. It is the tenth most popular language on GitHub, it ships by default with Ruby on Rails, and it has large followings in both client-side and server-side developer communities.

So what makes CoffeeScript special? Just like Goldilocks and her pilfered porridge, CoffeeScript derives its strength from being just right. It is a marked improvement over JavaScript; we'll spend much of this book learning how CoffeeScript can help us write code that is more concise, easier to read, and less prone to bugs. However, CoffeeScript does not overreach on features. CoffeeScript has little to no runtime of its own—there is no extra metadata to track, no extra memory management, no non-standard data structures. Instead, CoffeeScript compiles directly to ordinary-looking JavaScript, much like what an experienced JavaScript developer might write. In fact, CoffeeScript is less a new language than it is a shorthand for easily expressing the best practices of JavaScript.

CoffeeScript is an eminently pragmatic language, and this is the secret to its success. It's easy for JavaScript developers to learn, and most expertise carries over. It doesn't incur performance penalties over plain JavaScript. CoffeeScript and JavaScript can coexist peacefully, so it's easy to introduce CoffeeScript into existing JavaScript projects. Perhaps most importantly, CoffeeScript avoids the "magic" that is so often a source of bugs when the developer's assumptions don't match the language designer's assumptions. With CoffeeScript, it's very easy to understand what the resulting JavaScript will do and how it will behave.

It might also help that CoffeeScript is fun.

What this book covers

Chapter 1, Running a CoffeeScript Program, will cover installing the CoffeeScript tools and running a simple CoffeeScript program in both the console and a web browser.

Chapter 2, Writing Your First Lines of CoffeeScript, will explore the syntax of CoffeeScript and how it compiles to JavaScript.

In Chapter 3, Building a Simple Application, we will build an interactive web application and learn a few more CoffeeScript features along the way.

Chapter 4, Improving Our Application, will add more features to our web application, and explore more powerful CoffeeScript syntax.

Chapter 5, Using Classes, will teach us all about classes in CoffeeScript. It will also cover how to use them, how they work, and how to integrate with popular JavaScript frameworks.

In Chapter 6, Refactoring with Classes, we will use the new skills from previous chapter to refactor our web application using class-based structures.

In Chapter 7, Advanced CoffeeScript Features, we will learn advanced CoffeeScript features and idioms that reduce errors and make our code easier to understand. We will use them to add more features to our web application.

Chapter 8, Going Asynchronous, will show how CoffeeScript can help us deal with asynchronous operations, and integrate a third-party JavaScript library into our CoffeeScript application.

In Chapter 9, Debugging, we will learn how to use source maps to track problems in our application all the way back to the CoffeeScript source.

Chapter 10, Using CoffeeScript in More Places, will cover how to integrate CoffeeScript compilation into several popular web application frameworks.

In Chapter 11, CoffeeScript on the Server, we will run CoffeeScript on the server with Node.js, and learn how to integrate it with standard JavaScript Node modules.

What you need for this book

All you need for this book is a text editor and a working CoffeeScript compiler, and don't worry about the compiler—we'll cover installation and use of that tool in the first chapter! We provide instructions for using the tools on Windows, Mac OS X, and Linux. We'll be spending a lot of the book working on a client-side web application, so if you have any favorite development tools, feel free to bring those along. You'll also need a modern browser. The most recent version of Firefox or Chrome is ideal, but any other up-to-date browser such as Safari, Opera, or a recent Internet Explorer will also work fine.

Who this book is for

Some familiarity with the JavaScript language will help—CoffeeScript is a close relative, so it's useful to understand what the compiler's output is doing. It's also helpful, though not necessary, to have some experience with client-side web development. We'll be building a web application with a lot of CoffeeScript, plus a little HTML and CSS.

No experience with CoffeeScript is necessary. We'll cater to everyone from the total newbie to the person who has hacked together some CoffeeScript already but wants a better grasp of what's going on and how to best utilize the language.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can pull in another module by using the require function."

A block of code is set as follows:

fibonacci = (n) ->
  if n is 0 or n is 1
    n
  else
    fibonacci(n-1) + fibonacci(n-2)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

fibonacci = (n) ->
  if n is 0 or n is 1
    n
  else
    fibonacci(n-1) + fibonacci(n-2)

Any command-line input or output is written as follows:

coffee --compile --watch *.coffee

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.