CoffeeScript in Action
Patrick Lee
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email:
orders@manning.com2014 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co.20 Baldwin RoadPO Box 261Shelter Island, NY 11964 | Development editors: Renae Gregoire, Jennifer StoutCopyeditor: Linda RecktenwaldProofreaders: Andy Carroll, Katie TennantTechnical proofreader: Doug WarrenTypesetter: Dennis DalinnikIllustrator: Nick MarinoCover designer: Marija Tudor |
ISBN: 9781617290626
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 EBM 19 18 17 16 15 14
Brief Table of Contents
Table of Contents
Preface
Ive long thought that the things that will ultimately resonate the most with people dont reveal themselves immediately. Instead, they might initially present themselves as slightly interesting but not striking. Ive seen this with music, film, literature, and every other aspect of human experience that Ive looked at for any substantial amount of time. Ive 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 dont understand JavaScript! I was wrong about CoffeeScript just like I was wrong about JavaScript.
CoffeeScript is not about avoiding JavaScriptit 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.
Acknowledgments
Thanks to Jeremy Ashkenas for creating CoffeeScript. Thanks to Michael Stephens at Manning for picking up a book on CoffeeScript, and to my editors at ManningBert Bates, James Hatheway, Jennifer Stout, and Renae Gregoirewho worked with me at various stages of manuscript development. Thanks also to Kevin Sullivan, Linda Recktenwald, Andy Carroll, Katie Tennant, and Mary Piergies, as well as technical proofreader Doug Warren, who worked with me during productionI was very impressed. Finally, thanks to publisher Marjan Bace for having patience with a book that took much longer to complete than anybody had initially expected.
Thanks to all the family, friends, and colleagues who read and provided feedback on early drafts. Thanks to the ThoughtWorks Sydney office for their patience with me while I balanced consulting with authoring.
Thanks to Nick Marino for the illustrations that brought Agtron and Scruffy to life, and for making sense of my sometimes-bizarre scripts.
Thanks also to the MEAP readers who provided feedback on the chapters as they were being written, and to the following reviewers who read the chapters at various stages during the development of the manuscript: Andrew Broman, Brett Veenstra, Carl Witty, Carlos Santiago, Cleland Early, Daniel Bretoi, David Hunter, Guy Mackenzie, Ian Phillips, Jeff Foster, John Shea, Julian Parry, Ken Chien, Logan Johnson, Musannif Zahir, Olivier Broqueville, Peter Fries, Phily Austria, Robert Henley, and Sergey Seletskyy, and to Tim Moore and Mikkel Bergmann (who provided many important insights).
Finally, a special thanks to those who had to stand living with me while I wrote: thanks to Kandy and to Leigh.
About this Book
This is a language book. It doesnt 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, theyre all manufactured (contrived, if you will) slaves to the core goal of helping you learn to program in CoffeeScript. Youll find this book to be a useful reference because of its breadth and depth, but it isnt 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.
youll do the same with objects.
In youll enter the dragons lair of asynchronous programs.
In looks at the future and where you, the journeyman, are headed with CoffeeScript.
Prerequisites
This book doesnt 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 //: