CoffeeScript
Accelerated JavaScript Development
by Trevor Burnham
Version: P1.0 (July 2011)
Copyright 2011 Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trust-don't allow othersto use your copy of the book. Thanks.
- Dave & Andy.
Table of Contents
Copyright 2011, The Pragmatic Bookshelf.
What readers are saying about CoffeeScript: Accelerated JavaScript Development
Its hard to imagine a new web application today that doesnt make heavy use of JavaScript, but if youre used to something like Ruby, it feels like a significant step down to deal with JavaScript, more of a chore than a joy. Enter CoffeeScript: a pre-compiler that removes all the unnecessary verbosity of JavaScript and simply makes it a pleasure to write and read. Go, go, Coffee! This book is a great introduction to the world of CoffeeScript.
David Heinemeier Hansson |
Creator, Rails |
Just like CoffeeScript itself, Trevor gets straight to the point and shows you the benefits of CoffeeScript and how to write concise, clear CoffeeScript code.
Scott Leberknight |
Chief Architect, Near Infinity |
Though CoffeeScript is a new language, you can already find it almost everywhere. This book will show you just how powerful and fun CoffeeScript can be.
Stan Angeloff |
Managing Director, PSP WebTech Bulgaria |
This book helps readers become better JavaScripters in the process of learning CoffeeScript. Whats more, its a blast to read, especially if you are new to CoffeeScript and ready to learn.
Brendan Eich |
Creator, JavaScript |
CoffeeScript may turn out to be one of the great innovations in web application development; since I first discovered it, Ive never had to write a line of pure JavaScript. I hope the readers of this wonderful book will be able to say the same.
Dr. Nic Williams |
CEO/Founder, Mocra |
CoffeeScript: Accelerated JavaScript Development is an excellent guide to CoffeeScript from one of the communitys most esteemed members. Itll help you get up to speed with the language in no time, whether you write code that runs in the browser or on the server. Trevors book belongs on every CoffeeScript developers shelf.
Sam Stephenson |
Creator, Prototype JavaScript framework |
CoffeeScript is one of the most interesting developments in the world of programming languages in the last few years. Taking the lessons learned over the last decade from languages like Ruby and Python, it is a language with immense expressive power. CoffeeScript: Accelerated JavaScript Development is your guide to this new language and a must-read for those interested in being productive in JavaScript.
Travis Swicegood |
Author, Pragmatic Version Control Using Git |
Trevor serves up a rich blend of language overview and real-world examples, showcasing why I consider CoffeeScript my secret weapon for iOS, Android, and WebOS mobile development.
Wynn Netherland |
Co-host, The Changelog |
Fasten your seat belt and enjoy the ride with Trevor Burnham from JavaScript to CoffeeScript and have fun with web development again.
Javier Collado |
QA Automation Engineer, Canonical Ltd. |
Foreword
JavaScript is born free, but until recently, everywhere it was in chains.
JavaScript had never been a very pleasant language to work in: terribly slow, implemented with different quirks in different browsers, stuck fast in the amber of time since the late 1990s. Perhaps you used it in the past to implement a dropdown menu or a reorderable list, but you probably didnt enjoy the experience.
Fortunately for us, the JavaScript of today is enjoying a well-deserved renaissance. Thanks to the tireless efforts of browser implementers, its now the fastest mainstream dynamic language; its present everywhere, from servers to Photoshop, and its the only possible language you can use to program all angles of the web.
CoffeeScript is a little language that aims to give you easy access to the good parts of JavaScript: the first-class functions, the hash-like objects, even the much-misunderstood prototype chain. If we do our job right, youll end up writing one-third less code in order to generate much the same JavaScript you would have written in the first place.
CoffeeScript places a high value on the readability of code and the elimination of syntactic clutter. At the same time, theres a fairly one-to-one correspondence between CoffeeScript and JavaScript, which means that there should be no performance penaltyin fact, many JavaScript libraries end up running faster after being ported to CoffeeScript due to some of the optimizations the compiler can perform.
Youre fortunate to have picked up this book, because Trevor has been an enthusiastic contributor to CoffeeScript since the early days. Few people know more about the ins and outs of the language or the history of the debate behind language features and omissions than he does. This book is a gentle introduction to CoffeeScript led by an expert guide.
Im looking forward to hearing about all of the exciting projects that Im sure will come out of it, andwho knowsperhaps youll be inspired to create a little language of your very own.
Jeremy Ashkenas, creator of CoffeeScript
April 2011
Copyright 2011, The Pragmatic Bookshelf.
Acknowledgments
CoffeeScript is a young language. But from the start, its drawn an exceptionally diverse and spirited crowd. That wonderful energyon IRC, GitHub, Hacker News, blogs, Twitter, and elsewhereis what inspired me to write this book. To everyone who greeted CoffeeScript with enthusiasm in its infancy, I thank you.
Thanks, of course, to Jeremy Ashkenas for creating the language and contributing a generous foreword to this book; CoffeeScript could not have asked for a better BDFL. Thanks also to CoffeeScripts other contributors, who are too numerous to name here.
Thanks to the technical reviewersany remaining errors are completely and utterly my bad. I received helpful feedback from Javier Collado, Kevin Gisi, Darcy Laycock, Scott Leberknight, Sam Stephenson, Travis Swicegood, Federico Tomassetti, Stefan Turalski, and Dr. Nic Williams. Special shout-outs to Jeremy Ashkenas (again) and Michael Ficarra, core contributors to the CoffeeScript project who took time from their busy schedules to set me straight on many of the languages finer points. Thanks also to Brendan Eich, the creator of JavaScript, who graciously clarified several points.
Thanks to the Pragmatic Bookshelf crowd. First and foremost to Michael Swaine, whom Im proud to call my editor. Thanks also to managing editor Susannah Pfalzer and to bigwigs Dave Thomas and Andy Hunt for taking a chance on a book on a lesser-known language from an even less-known author.
Thanks, finally, to Scott and Teresa Burnham, more commonly referred to by me and at least two other people as Dad and Mom. Their support, and their example, has been valuable beyond measure.