1. Trang chủ
  2. » Công Nghệ Thông Tin

CoffeeScript: Accelerated JavaScript Development pdf

154 793 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 154
Dung lượng 2,69 MB

Nội dung

www.it-ebooks.info What readers are saying about CoffeeScript: Accelerated JavaScript Development It’s hard to imagine a new web application today that doesn’t make heavy use of JavaScript, but if you’re 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 www.it-ebooks.info This book helps readers become better JavaScripters in the process of learning CoffeeScript. What’s more, it’s a blast to read, especially if you are new to Coffee- Script 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, I’ve 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 Coffee- Script from one of the community’s most esteemed members. It’ll 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. Trevor’s book belongs on every CoffeeScript developer’s shelf. ➤ Sam Stephenson Creator, Prototype JavaScript framework www.it-ebooks.info CoffeeScript is one of the most interesting developments in the world of program- ming 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 ex- pressive 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. www.it-ebooks.info CoffeeScript Accelerated JavaScript Development Trevor Burnham The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina www.it-ebooks.info Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com. The team that produced this book includes: Michael Swaine (editor) Potomac Indexing, LLC (indexer) Kim Wimpsett (copyeditor) David Kelly (typesetter) Janet Furlow (producer) Juliet Benda (rights) Ellie Callahan (support) Copyright © 2011 Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or tra nsmit te d, in any form, or by a ny mean s, el ec troni c, mechani ca l, ph ot ocopy in g, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-934356-78-4 Printed on acid-free paper. Book version: P1.0—July 2011 www.it-ebooks.info Contents Foreword . . . . . . . . . . . . . xi Acknowledgments . . . . . . . . . . xiii Preface . . . . . . . . . . . . . xv 1. Getting Started . . . . . . . . . . . . 1 Installing CoffeeScript 11.1 1.2 Text Editors for CoffeeScript 5 1.3 Meet ’coffee’ 6 1.4 Debugging CoffeeScript 9 2. Functions, Scope, and Context . . . . . . . 13 Functions 101 132.1 2.2 Scope: Where You See ’Em 18 2.3 Context (or, “What Is ’this’?”) 21 2.4 Property Arguments (@arg) 24 2.5 Default Arguments (arg =) 25 2.6 Splats ( ) 28 2.7 Project: 5x5 Input Parser 29 2.9 Exercises 34 3. Collections and Iteration . . . . . . . . . 37 Objects as Hashes 373.1 3.2 Arrays 40 3.3 Iterating over Collections 43 3.4 Conditional Iteration 46 3.5 Comprehensions 47 3.6 Pattern Matching (or, Destructuring Assignment) 48 3.7 Project: 5x5 Solitaire 50 3.9 Exercises 56 www.it-ebooks.info 4. Modules and Classes . . . . . . . . . . 59 Modules: Splitting Up Apps 604.1 4.2 The Power of Prototypes 61 4.3 Classes: Functions with Prototypes 63 4.4 Inheritance with ’extends’ 65 4.5 Project: Refactoring 5x5 68 4.7 Exercises 72 5. Web Interactivity with jQuery . . . . . . . . 75 The Tao of jQuery 765.1 5.2 Manipulating the DOM 76 5.3 Getting Selective 77 5.4 Reacting to Events 79 5.5 Project: Browser-Based 5x5 80 5.7 Exercises 88 6. Server-Side Apps with Node.js . . . . . . . . 91 What Is Node.js? 916.1 6.2 Modularizing Code with ’exports’ and ’require’ 92 6.3 Thinking Asynchronously 93 6.4 Project: Multiplayer 5x5 97 6.6 Exercises 105 A1. Answers to Exercises . . . . . . . . . 107 Functions, Scope, and Context 107A 1 . 1 A 1 . 2 Collections and Iteration 109 A 1 . 3 Modules and Classes 111 A 1 . 4 Web Interactivity with jQuery 112 A 1 . 5 Server-Side Apps with Node.js 113 A2. Ways of Running CoffeeScript . . . . . . . 115 Web Consoles 115A 2 . 1 A 2 . 2 Running CoffeeScript in Your Web App 116 A 2 . 3 CoffeeScript on Rails 116 A 2 . 4 CoffeeScript via Middleware 117 A 2 . 5 CoffeeScript on Node.js 117 A 2 . 6 Rapid Websites with Middleman 118 A 2 . 7 CoffeeScript for System Scripts 119 • viii www.it-ebooks.info A3. Cheat Sheet for JavaScripters . . . . . . . 121 Boolean Operators 121A 3 . 1 A 3 . 2 The Existential Operator 121 A 3 . 3 Context and Prototype Accessors 122 A 3 . 4 Function Definitions 122 A 3 . 5 Conditionals 122 A 3 . 6 Property Existence 122 A 3 . 7 Iteration 123 A4. Bibliography . . . . . . . . . . . . 125 Index . . . . . . . . . . . . . 127 • ix www.it-ebooks.info • x www.it-ebooks.info [...]... a more agile, iterative development style It’s worth adding that switching to CoffeeScript isn’t an all-or-nothing proposition—CoffeeScript code and JavaScript code can interact freely CoffeeScript’s strings are just JavaScript strings, and its numbers are just JavaScript numbers; even its classes work in JavaScript frameworks like Backbone.js.5 So don’t be afraid of calling JavaScript code from CoffeeScript... However, because CoffeeScript is so closely linked to JavaScript, there are really two languages running through this book—and not enough pages to teach you both Therefore, I’m going to assume that you know some JavaScript You don’t have to be John JavaScript Ninja” Resig In fact, if you’re only an amateur JavaScripter, great! You’ll learn a lot about JavaScript as you go through this book Check the footnotes... -p, -l, compile interactive output join watch print lint compile to JavaScript and save as js files run an interactive CoffeeScript REPL set the directory for compiled JavaScript concatenate the scripts before compiling watch scripts for changes, and recompile print the compiled JavaScript to stdout pipe the compiled JavaScript through JSLint www.it-ebooks.info Meet ’coffee’ -s, -e, -r, -b,... help message So if you wanted to see the JavaScript that the compiler hid from you just now, you’d run this: $ coffee -p hello.coffee (function() { console.log('Hello, world!'); }).call(this); See JavaScript, Under Wraps, on page 8 for an explanation of those extra two lines Compiling to JavaScript Probably the most common flag is -c (“compile”), which saves the JavaScript output to a file The file is... app for thousands of users Sadly, that’s the world we live in JavaScript doesn’t have a standard interpreter Instead, hundreds of browsers and server-side frameworks run JavaScript in their own way Debugging cross-platform inconsistencies is a huge pain CoffeeScript can’t cure all of these ills, but the compiler tries its best to generate JavaScript Lint-compliant output3, which is a great filter for... 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, there’s a fairly one-to-one correspondence between CoffeeScript and JavaScript, which means that there should be no performance penalty—in fact, many JavaScript libraries end up running faster... example, we’ll talk about using CoffeeScript with one of JavaScript s most popular libraries in Chapter 5, Web Interactivity with jQuery, on page 75 4 5 http://www.paulgraham.com/power.html http://documentcloud.github.com/backbone/ www.it-ebooks.info • xviii Embedding JavaScript in CoffeeScript This is as good a place as any to mention that you can stick JavaScript inside of CoffeeScript code by surrounding... was no controlling it As the only language understood by all major browsers, JavaScript quickly became the lingua franca of the Web And with the introduction of Ajax in the early 2000s, what began as a humble scripting language for enhancing web pages suddenly became a full-fledged rich application development language As JavaScript s star rose, discontent came from all corners Some pointed to its... postfix if/unless And thanks to Rails 3.1, CoffeeScript has a huge following 6 7 8 9 http://www.codinghorror.com/blog/2009/07/meta-is-murder.html http://eloquentjavascript.net/ http://javascriptgarden.info/ https://developer.mozilla.org/en /JavaScript/ Guide www.it-ebooks.info • xix in the Ruby world So if you’re a Rubyist, great! You’ve got a head start If not, don’t sweat it; everything will fall into... (see the next section) To master CoffeeScript, you’ll need to know how it works with the rest of the JavaScript universe So after learning the basics of the language, we’ll take brief tours of jQuery, the world’s most popular JavaScript framework, and Node.js, an exciting new project that lets you run JavaScript outside of the browser While we won’t go into great depth with either tool, we’ll see that . saying about CoffeeScript: Accelerated JavaScript Development It’s hard to imagine a new web application today that doesn’t make heavy use of JavaScript, . 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

Ngày đăng: 15/03/2014, 18:20

TỪ KHÓA LIÊN QUAN