CoffeeScript in Action
Patrick Lee
  • May 2014
  • ISBN 9781617290626
  • 432 pages
  • printed in black & white

This book will help you become a CoffeeScript Ninja!

Phily Austria, Paystr LLC

CoffeeScript in Action is a tutorial that teaches you how, where, and why to use CoffeeScript. It begins by quickly exposing you to CoffeeScript's new, but instantly-familiar syntax. Then, you'll explore programming challenges that illustrate CoffeeScript's unique advantages. Language junkies will especially appreciate how this book takes idioms from other programming languages and shows you how CoffeeScript makes them available in JavaScript.

Table of Contents detailed table of contents

preface

acknowledgments

about this book

about the cover illustration

Part 1 Foundations

1. The road to CoffeeScript

1.1. Why CoffeeScript?

1.2. Running CoffeeScript

1.3. JavaScript

1.3.1. C

1.3.2. Scheme

1.3.3. Self

1.4. Evolving JavaScript

1.4.1. A little story about language

1.4.2. The lesson for JavaScript

1.5. Creating CoffeeScript

1.5.1. Designing new syntax

1.5.2. Achieving new syntax

1.6. Summary

2. Simplified syntax

2.1. Your first program

2.1.1. Comparing CoffeeScript to JavaScript

2.2. Simple expressions

2.2.1. Literal values

2.2.2. Variables

2.3. Operators

2.3.1. Essentials

2.3.2. Types, existential, and combining operators

2.3.3. Exercises

2.4. Statements

2.4.1. Anatomy

2.4.2. Statements as expressions

2.4.3. Pure statements

2.4.4. Exercise

2.5. Strings

2.5.1. Methods

2.5.2. Interpolation

2.5.3. Exercise

2.6. Arrays

2.6.1. length, join, slice, and concat

2.6.2. in

2.6.3. Ranges

2.6.4. Comprehensions

2.6.5. Exercise

2.7. Heres for comments, docs, and regexes

2.7.1. Comments

2.7.2. Heredocs

2.7.3. Heregexes

2.8. Putting it together

2.8.1. Running in a browser

2.8.2. Running on the command line

2.9. Summary

3. First-class functions

3.1. Computation

3.1.1. Basics

3.1.2. Custom operations

3.1.3. Anatomy

3.1.4. Comparison: JavaScript and CoffeeScript

3.1.5. Exercises

3.2. Events

3.2.1. Browser events

3.2.2. Using timeouts and intervals to create events

3.3. I/O

3.3.1. Ajax

3.3.2. Event-driven file reading with Node.js

3.3.3. Event-driven file serving with Node.js

3.3.4. Exercises

3.4. Higher-order functions

3.4.1. Invoking other functions

3.4.2. Example: Counting words in a file

3.4.3. Functions as arguments

3.4.4. Exercises

3.5. Scope

3.5.1. Lexical function scope

3.5.2. No block scope

3.5.3. Implicit variable declarations

3.5.4. Nesting

3.6. Closures

3.6.1. Global state problems

3.6.2. Functions as return values

3.6.3. Extended example: using closure

3.7. Putting it together

3.8. Summary

4. Dynamic objects

4.1. Syntax

4.1.1. Literals

4.1.2. Properties

4.1.3. YAML-style syntax

4.2. Key-value stores

4.2.1. Data

4.2.2. Key-values for named arguments

4.2.3. Exercises

4.3. Comprehensions

4.3.1. Object comprehensions

4.3.2. Example

4.4. Structured data

4.4.1. JSON

4.4.2. Trees

4.5. Binding

4.5.1. this

4.5.2. The fat arrow

4.6. Prototypes

4.6.1. Copy

4.6.2. Object creation

4.6.3. Exercises

4.7. Behavior

4.7.1. Refactor

4.7.2. Exercise

4.8. Classes

4.8.1. Declaration

4.8.2. Object identity

4.8.3. Exercises

4.9. Putting it together

4.10. Summary

Part 2 Composition

5. Composing objects

5.1. Being classical

5.1.1. Raw data

5.1.2. Class abstractions

5.2. Class inheritance

5.2.1. extends

5.2.2. How does it work?

5.3. Class variables and properties

5.3.1. Usage

5.3.2. Declaring (to keep things together)

5.3.3. Exercise

5.4. Overriding and super

5.4.1. Overriding

5.4.2. super

5.5. Modifying prototypes

5.5.1. How class declarations work

5.5.2. How methods work

5.5.3. Dynamic classes

5.6. Extending built-ins

5.6.1. Built-in constructor prototypes

5.6.2. Extending date

5.6.3. Don’t modify objects you don’t own

5.7. Mixins

5.7.1. Class inheritance can be awkward

5.7.2. Writing a mixin

5.7.3. Example: enumerable mixin

5.7.4. Mixins from null

5.8. Putting it together

5.8.1. Exercise

5.9. Summary

6. Composing functions

6.1. Clarity

6.1.1. Functions are descriptions

6.1.2. Where arguments need parentheses

6.1.3. Higher-order functions revisited

6.2. State and mutability

6.2.1. Variables, assignment, and side effects

6.2.2. Local state and shared state

6.2.3. Encapsulating state with objects

6.2.4. World state

6.3. Abstraction

6.3.1. Extracting common code

6.3.2. Adding common code

6.3.3. Recursion

6.4. Combinators

6.4.1. Compose

6.4.2. Before and after

6.4.3. Around

6.4.4. Working with objects

6.4.5. Asynchronous combinators

6.5. Summary

7. Style and semantics

7.1. Rest and spread parameters

7.1.1. Rest

7.1.2. Spread

7.2. Destructuring

7.2.1. Arrays

7.2.2. Objects

7.2.3. Object shorthand

7.2.4. Array destructuring expansion

7.2.5. Exercises

7.2.6. Putting it together

7.3. No nulls

7.3.1. Null soak

7.3.2. Conditional assignment

7.4. No types — the duck test

7.4.1. Don’t rely on typeof, instanceof, or constructor

7.4.2. How to use duck typing

7.5. When to use comprehensions (and when not to)

7.5.1. map

7.5.2. filter

7.5.3. reduce

7.5.4. Defining functions inside comprehensions

7.6. Fluent interfaces

7.6.1. Why create them?

7.6.2. The indentation problem

7.6.3. Creating fluent interfaces

7.6.4. Chain

7.7. Ambiguity

7.7.1. Whitespace and indentation

7.7.2. Implicit variables

7.8. Summary

8. Metaprogramming

8.1. Literate CoffeeScript

8.1.1. The .litcoffee file extension

8.2. Domain-specific languages

8.2.1. External DSLs

8.2.2. Internal DSLs

8.2.3. Object literals

8.2.4. Fluent interfaces

8.2.5. Function passing

8.2.6. Constructing a DSL

8.3. How the compiler works

8.3.1. Tokenizing

8.3.2. Rewriting

8.3.3. The abstract syntax tree

8.4. Bending code to your ideas

8.4.1. Can you just eval?

8.4.2. Rewriting the token stream

8.4.3. Using the abstract syntax tree

8.4.4. It’s just JavaScript

8.5. Summary

9. Composing the asynchronous

9.1. Data processing

9.1.1. Reading

9.1.2. Sorting

9.1.3. Performance

9.1.4. Decorate, sort, undecorate

9.2. Event loops

9.2.1. Events and blackouts

9.2.2. Infinite time

9.3. Event emitters

9.3.1. User events

9.3.2. Data as events

9.3.3. Using event emitters in Node.js

9.3.4. Events as data

9.4. Event composition

9.4.1. Lazy data handling

9.4.2. Lazy event handling

9.4.3. Composing event streams

9.4.4. Client side

9.4.5. Multiple event sources

9.5. Summary

Part 3 Applications

10. Driving with tests

10.1. No tests? Disaster awaits

10.2. How to write tests

10.2.1. Assertions

10.2.2. How to unit test

10.2.3. Rinse and repeat

10.2.4. Feedback

10.3. Dependencies

10.3.1. Why dependencies make testing difficult

10.3.2. Test doubles

10.3.3. Avoiding dependency injection hell

10.4. Testing the asynchronous

10.4.1. Live with it

10.4.2. Remove it

10.4.3. Expect it

10.4.4. Exercise

10.5. System tests

10.6. Test suites

10.6.1. Setups and teardowns

10.6.2. Test helpers and runners

10.6.3. Watchers

10.7. Summary

11. In the browser

11.1. Getting started

11.1.1. Manual compilation

11.1.2. Browser compilation

11.1.3. Automatic compilation

11.2. Communicating with the outside world

11.2.1. Using XMLHttpRequest

11.2.2. Dynamic script insertion

11.2.3. Going real time with WebSocket

11.3. Cross-browser compatibility

11.3.1. Polyfilling host objects

11.3.2. Polyfilling language features

11.4. Creating a user interface

11.4.1. Retained mode with the DOM

11.4.2. Immediate mode with HTML5 canvas

11.5. Creating animations

11.5.1. Retained mode

11.5.2. Immediate mode

11.6. Structuring programs

11.6.1. Abstraction and APIs

11.6.2. Dealing with time

11.7. Summary

12. Modules and builds

12.1. Server-side modules (on Node.js)

12.1.1. Creating and requiring

12.1.2. Exporting

12.1.3. No file extensions

12.1.4. The module cache

12.1.5. Putting it together

12.1.6. Indexes

12.2. Build automation with Cake

12.2.1. Cake and build tasks

12.2.2. Test tasks

12.2.3. Task dependencies

12.3. Client-side modules (in a web browser)

12.3.1. Making modules work in a browser

12.3.2. How to write a module system

12.3.3. Tests

12.4. Application deployment

12.4.1. Creating an artifact (something that’s easy to deploy)

12.4.2. Creating a manifest (something that tells your artifact where it is)

12.5. The final Cakefile

12.5.1. Tidying up

12.6. Summary

13. ECMAScript and the future of CoffeeScript

13.1. CoffeeScript in the context of JavaScript

13.1.1. A better JavaScript through CoffeeScript

13.1.2. Future JavaScript features that CoffeeScript has today

13.2. ECMAScript 5

13.2.1. Runtime support

13.2.2. Object.create

13.2.3. JSON

13.2.4. Property descriptors

13.2.5. Putting it together

13.2.6. Strict mode

13.3. ECMAScript 6

13.3.1. Modules

13.3.2. const and let

13.3.3. Sets, Maps, and WeakMaps

13.3.4. Proxies

13.3.5. Comprehensions, iterators, and generators

13.4. Source maps for debugging

13.4.1. Why source maps?

13.4.2. Getting started with source maps

13.5. Summary

Appendix A: Reserved words

Appendix B: Answers to exercises

Appendix C: Popular libraries

index

About the Technology

CoffeeScript preserves everything you know and love about JavaScript and wraps it in a modern, efficient syntax. It's perfect for complex web programming, including server-side JavaScript executing on Node.js. CoffeeScript also enhances JavaScript's natural Functional Programming features, a big advantage for building high-capacity, fault-tolerant applications. CoffeeScript is fully supported in Rails 3.1, and, via a free plugin, even Microsoft Visual Studio. Plus, it's really cool.

About the book

JavaScript runs (almost) everywhere but it can be quirky and awkward. Its cousin CoffeeScript is easier to comprehend and compose. An expressive language, not unlike Ruby or Python, it compiles into standard JavaScript without modification and is a great choice for complex web applications. It runs in any JavaScript-enabled environment and is easy to use with Node.js and Rails.

CoffeeScript in Action teaches you how, where, and why to use CoffeeScript. It immerses you in CoffeeScript's comfortable syntax before diving into the concepts and techniques you need in order to write elegant CoffeeScript programs. Throughout, you'll explore programming challenges that illustrate CoffeeScript's unique advantages. For language junkies, the book explains how CoffeeScript brings idioms from other languages into JavaScript.

What's inside

  • CoffeeScript's syntax and structure
  • Web application patterns and best practices
  • Prototype-based OOP
  • Functional programming
  • Asynchronous programming techniques
  • Builds and testing

About the reader

Readers need a basic grasp of web development and how JavaScript works. No prior exposure to CoffeeScript is required.

About the author

Patrick Lee is a developer, designer, and software consultant, working with design startup Canva in Sydney, Australia


Buy
  • combo $44.99 pBook + eBook
  • eBook $35.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

Truly entertaining ... dives deep into CoffeeScript.

Andrew Broman, University of Wisconsin, Madison

By far the best resource for learning CoffeeScript or for improving your existing skills.

John Shea, Endicott College

Makes learning CoffeeScript fun!

Kenrick Chien, Blue Star Software