ebook img

Eloquent JavaScript: A Modern Introduction to Programming PDF

450 Pages·2018·16.98 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Eloquent JavaScript: A Modern Introduction to Programming

Master the Language of the Web 3rd Edition E JavaScript lies at the heart of almost every a browser game, a simple programming lan- l modern web application, from social apps like guage, and a paint program, you’ll learn how to: o Twitter to browser-based game frameworks.  Understand the essential elements of pro- Though simple for beginners to pick up and gramming, including syntax, control, and data q play with, JavaScript is a flexible, complex  Organize and clarify your code with object- language that you can use to build full-scale u third Edition oriented and functional programming applications. techniques e This third edition of Eloquent JavaScript  Script the browser and make basic web dives deep into the JavaScript language to show n applications you how to write beautiful, effective code. The A Modern Introduction updates include brand-new material on features  Use the DOM effectively to interact with t like class notation, arrow functions, iterators, browsers to Programming async functions, template strings, and block J  Harness Node.js to build servers and utilities scope. a Isn’t it time you became fluent in the language Haverbeke immerses you in code from the v Marijn Haverbeke of the web? start and teaches through extensive examples, a while exercises and full-chapter projects give About the Author S you hands-on experience writing your own programs. First you’ll learn the basic structure Marijn Haverbeke is a programming language c of the JavaScript language as well as control enthusiast and polyglot. He’s worked on a wide r structures, functions, and data structures. range of software systems, from databases to Then you’ll learn about error handling and compilers to editors. He runs a small business i bug fixing, modularity, and asynchronous pro- around his open source projects. p gramming before moving on to programming t web browsers. As you build projects such as All source code is available online in an interactive sandbox, where you can edit the code, run it, and see its output instantly. H a v e r b THE FINEST IN GEEK ENTERTAINMENT™ Price: $39.95 ($53.95 CDN) www.nostarch.com e Shelve In: Programming Languages/JavaScript k e Praise for the first and second editions of EloquEnt JavaScript “Provides some of the best explanations of programming concepts that I’ve ever read.” —Sandra Henry-Stocker, It World “If you choose to start your JavaScript journey with this book, it can quickly teach you a lot of technical information and also programming wisdom.” —MIcHael J. roSS, Web developer and SlaSHdot contrIbutor “I became a better architect, author, mentor and developer because of this book. It deserves to share shelf space with Flannagan and Crockford.” —anguS croll, tWItter developer “The best introduction into any programming language and programming overall. Period.” —Jan leHnardt, co-creator of HoodIe and organIzer of JSconf eu “This is the book I give out when people ask me how to learn proper JavaScript.” —cHrIS WIllIaMS, organIzer of JSconf uS “One of the best JavaScript books I’ve read.” —rey bango, JQuery teaM MeMber and clIent-Web coMMunIty prograM Manager at MIcroSoft “A really good guide to JavaScript; but even more than that, this book is a great guide to programming.” —ben nadel, cHIef SoftWare engIneer at epIcenter conSultIng “A good book, suitable for those without experience in JavaScript and even those without programming experience.” —nIcHolaS zakaS, autHor of HigH Performance JavaScriPt and tHe PrinciPleS of obJect-oriented JavaScriPt “If you’re new to JavaScript, the first thing I’d recommend you do is visit Eloquent JavaScript and check out Marijn Haverbeke’s introduction to the language.” —cnet uk E l o q u E n t J a v a S c r i p t a Modern introduction to programming by Marijn haverbeke San Francisco EloquEnt JavaScript, 3rd Edition. Copyright © 2019 by Marijn Haverbeke. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-10: 1-59327-950-7 ISBN-13: 978-1-59327-950-9 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Madalina Tantareanu Interior Design: Octopod Studios Developmental Editor: Corbin Collins Technical Reviewer: Angus Croll Copyeditor: Kim Wimpsett Compositor: Riley Hoffman Proofreader: James M. Fraleigh Chapter illustrations by Madalina Tantareanu. Pixel art in Chapters 7 and 16 by Antonio Perdomo Pastor. Regular expression diagrams in Chapter 9 generated with Regexper by Jeff Avallone (http:// regexper.com/). Village photograph in Chapter 11 by Fabrice Creuzot. Game concept for Chapter 15 by Thomas Palef (http://lessmilk.com/). For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900; [email protected] www.nostarch.com The Library of Congress has catalogued the first edition as follows: Haverbeke, Marijn. Eloquent JavaScript: a modern introduction to programming / by Marijn Haverbeke. p. cm. Includes index. ISBN-13: 978-1-59327-282-1 ISBN-10: 1-59327-282-0 1. JavaScript (Computer program language) I. Title. QA76.73.J39H38 2009 005.13'3--dc22 2010032246 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. BRIEF CONTENTS Introduction......................................................................... 1 PARTI:LANGUAGE Chapter1:Values,Types,andOperators.............................................. 11 Chapter2:ProgramStructure......................................................... 23 Chapter3:Functions................................................................. 41 Chapter4:DataStructures: ObjectsandArrays........................................ 59 Chapter5:Higher-OrderFunctions.................................................... 83 Chapter6:TheSecretLifeofObjects.................................................. 97 Chapter7:Project: ARobot.......................................................... 117 Chapter8:BugsandErrors .......................................................... 129 Chapter9:RegularExpressions....................................................... 145 Chapter10:Modules................................................................ 167 Chapter11:AsynchronousProgramming .............................................. 181 Chapter12:Project: AProgrammingLanguage ........................................ 203 PARTII:BROWSER Chapter13:JavaScriptandtheBrowser............................................... 219 Chapter14:TheDocumentObjectModel ............................................. 227 Chapter15:HandlingEvents......................................................... 247 Chapter16:Project: APlatformGame ................................................ 265 Chapter17:DrawingonCanvas ..................................................... 287 Chapter18:HTTPandForms......................................................... 311 Chapter19:Project: APixelArtEditor ................................................ 333 PARTIII:NODE Chapter20:Node.js ................................................................ 353 Chapter21:Project: Skill-SharingWebsite............................................. 371 Chapter22:JavaScriptandPerformance .............................................. 391 ExerciseHints....................................................................... 407 Index............................................................................... 429 vi BriefContents CONTENTS IN DETAIL INTRODUCTION 1 OnProgramming................................................................. 2 WhyLanguageMatters ........................................................... 3 WhatIsJavaScript? .............................................................. 5 Code,andWhattoDowithIt ..................................................... 6 OverviewofThisBook............................................................ 7 TypographicConventions ......................................................... 8 PART I: LANGUAGE 1 VALUES, TYPES, AND OPERATORS 11 Values .......................................................................... 12 Numbers ........................................................................ 12 Arithmetic............................................................... 13 SpecialNumbers ........................................................ 14 Strings .......................................................................... 14 UnaryOperators ................................................................. 16 BooleanValues .................................................................. 16 Comparison............................................................. 16 LogicalOperators........................................................ 17 EmptyValues .................................................................... 18 AutomaticTypeConversion ....................................................... 18 Short-CircuitingofLogicalOperators....................................... 19 Summary ........................................................................ 20 2 PROGRAM STRUCTURE 23 ExpressionsandStatements ....................................................... 23 Bindings......................................................................... 24 BindingNames .................................................................. 26 TheEnvironment.................................................................. 26 Functions ........................................................................ 26 Theconsole.logFunction.......................................................... 27 ReturnValues .................................................................... 27 ControlFlow..................................................................... 28 ConditionalExecution............................................................. 28 whileanddoLoops............................................................... 30 IndentingCode .................................................................. 32 forLoops ........................................................................ 32 BreakingOutofaLoop ........................................................... 33 UpdatingBindingsSuccinctly...................................................... 34 DispatchingonaValuewithswitch ................................................ 34 Capitalization.................................................................... 35 Comments....................................................................... 35 Summary ........................................................................ 36 Exercises ........................................................................ 37 LoopingaTriangle....................................................... 37 FizzBuzz................................................................ 37 Chessboard ............................................................. 38 3 FUNCTIONS 41 DefiningaFunction............................................................... 41 BindingsandScopes ............................................................. 43 NestedScope ........................................................... 44 FunctionsasValues............................................................... 44 DeclarationNotation ............................................................. 45 ArrowFunctions.................................................................. 45 TheCallStack ................................................................... 46 OptionalArguments .............................................................. 47 Closure.......................................................................... 48 Recursion........................................................................ 50 GrowingFunctions ............................................................... 52 FunctionsandSideEffects......................................................... 54 Summary ........................................................................ 55 Exercises ........................................................................ 56 Minimum................................................................ 56 Recursion ............................................................... 56 BeanCounting .......................................................... 56 4 DATA STRUCTURES: OBJECTS AND ARRAYS 59 TheWeresquirrel................................................................. 60 DataSets........................................................................ 60 Properties........................................................................ 61 Methods......................................................................... 62 Objects ......................................................................... 62 Mutability ....................................................................... 65 TheLycanthrope’sLog ............................................................ 66 ComputingCorrelation............................................................ 67 ArrayLoops ..................................................................... 69 viii ContentsinDetail

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.