Node.Js 562 Background
Total Page:16
File Type:pdf, Size:1020Kb
Eloquent JavaScript 3rd edition Marijn Haverbeke Copyright © 2018 by Marijn Haverbeke This work is licensed under a Creative Commons attribution-noncommercial license (http://creativecommons.org/licenses/by-nc/3.0/). All code in the book may also be considered licensed under an MIT license (https: //eloquentjavascript.net/code/LICENSE). The illustrations are contributed by various artists: Cover and chap- ter illustrations by Madalina Tantareanu. Pixel art in Chapters 7 and 16 by Antonio Perdomo Pastor. Regular expression diagrams in Chap- ter 9 generated with regexper.com by Jeff Avallone. Village photograph in Chapter 11 by Fabrice Creuzot. Game concept for Chapter 16 by Thomas Palef. The third edition of Eloquent JavaScript was made possible by 325 financial backers. You can buy a print version of this book, with an extra bonus chapter in- cluded, printed by No Starch Press at http://a-fwd.com/com=marijhaver- 20&asin-com=1593279507. i Contents Introduction 1 On programming .......................... 2 Why language matters ....................... 4 What is JavaScript? ......................... 9 Code, and what to do with it ................... 11 Overview of this book ........................ 12 Typographic conventions ...................... 13 1 Values, Types, and Operators 15 Values ................................. 16 Numbers ............................... 17 Strings ................................ 21 Unary operators ........................... 24 Boolean values ............................ 25 Empty values ............................. 29 Automatic type conversion ..................... 29 Summary ............................... 33 ii 2 Program Structure 34 Expressions and statements .................... 34 Bindings ............................... 36 Binding names ............................ 39 The environment .......................... 40 Functions ............................... 40 The console.log function ...................... 41 Return values ............................ 42 Control flow ............................. 43 Conditional execution ........................ 44 while and do loops .......................... 47 Indenting Code ........................... 50 for loops ............................... 51 Breaking Out of a Loop ...................... 52 Updating bindings succinctly ................... 53 Dispatching on a value with switch ................ 54 Capitalization ............................ 56 Comments .............................. 57 Summary ............................... 58 Exercises ............................... 59 3 Functions 62 Defining a function ......................... 63 Bindings and scopes ......................... 65 Functions as values ......................... 68 Declaration notation ........................ 68 Arrow functions ........................... 70 iii The call stack ............................ 71 Optional Arguments ......................... 73 Closure ................................ 76 Recursion ............................... 78 Growing functions .......................... 83 Functions and side effects ..................... 87 Summary ............................... 88 Exercises ............................... 89 4 Data Structures: Objects and Arrays 91 The weresquirrel ........................... 92 Data sets ............................... 93 Properties .............................. 94 Methods ............................... 95 Objects ................................ 97 Mutability .............................. 101 The lycanthrope’s log ........................ 103 Computing correlation ....................... 106 Array loops .............................. 109 The final analysis .......................... 110 Further arrayology .......................... 113 Strings and their properties .................... 116 Rest parameters ........................... 119 The Math object ........................... 120 Destructuring ............................ 123 JSON ................................. 124 Summary ............................... 126 iv Exercises ............................... 127 5 Higher-Order Functions 131 Abstraction .............................. 132 Abstracting repetition ....................... 134 Higher-order functions ....................... 136 Script data set ............................ 138 Filtering arrays ........................... 139 Transforming with map ....................... 141 Summarizing with reduce ...................... 142 Composability ............................ 144 Strings and character codes .................... 146 Recognizing text ........................... 149 Summary ............................... 152 Exercises ............................... 152 6 The Secret Life of Objects 155 Encapsulation ............................ 155 Methods ............................... 156 Prototypes .............................. 159 Classes ................................ 161 Class notation ............................ 164 Overriding derived properties ................... 165 Maps ................................. 167 Polymorphism ............................ 170 Symbols ................................ 171 The iterator interface ........................ 173 v Getters, setters, and statics .................... 177 Inheritance .............................. 180 The instanceof operator ....................... 182 Summary ............................... 183 Exercises ............................... 184 7 Project: A Robot 187 Meadowfield ............................. 187 The task ............................... 190 Persistent data ............................ 193 Simulation .............................. 194 The mail truck’s route ....................... 197 Pathfinding .............................. 198 Exercises ............................... 202 8 Bugs and Errors 204 Language ............................... 204 Strict mode .............................. 205 Types ................................. 207 Testing ................................ 209 Debugging .............................. 211 Error propagation .......................... 213 Exceptions .............................. 215 Cleaning up after exceptions .................... 218 Selective catching .......................... 221 Assertions .............................. 224 Summary ............................... 225 vi Exercises ............................... 226 9 Regular Expressions 228 Creating a regular expression ................... 229 Testing for matches ......................... 230 Sets of characters .......................... 230 Repeating parts of a pattern .................... 233 Grouping subexpressions ...................... 234 Matches and groups ......................... 235 The Date class ............................ 237 Word and string boundaries .................... 239 Choice patterns ........................... 240 The mechanics of matching .................... 241 Backtracking ............................. 243 The replace method ......................... 246 Greed ................................. 249 Dynamically creating RegExp objects .............. 251 The search method ......................... 252 The lastIndex property ....................... 253 Parsing an INI file .......................... 256 International characters ....................... 260 Summary ............................... 262 Exercises ............................... 264 10 Modules 267 Modules ................................ 268 Packages ............................... 269 vii Improvised modules ......................... 271 Evaluating data as code ...................... 272 CommonJS .............................. 273 ECMAScript modules ........................ 277 Building and bundling ....................... 280 Module design ............................ 281 Summary ............................... 284 Exercises ............................... 285 11 Asynchronous Programming 287 Asynchronicity ............................ 288 Crow tech ............................... 290 Callbacks ............................... 291 Promises ............................... 295 Failure ................................ 297 Networks are hard .......................... 300 Collections of promises ....................... 303 Network flooding .......................... 305 Message routing ........................... 306 Async functions ........................... 310 Generators .............................. 314 The event loop ............................ 316 Asynchronous bugs ......................... 318 Summary ............................... 320 Exercises ............................... 321 viii 12 Project: A Programming Language 323 Parsing ................................ 323 The evaluator ............................ 330 Special forms ............................. 333 The environment .......................... 335 Functions ............................... 338 Compilation ............................. 340 Cheating ............................... 341 Exercises ............................... 343 13 JavaScript and the Browser 346 Networks and the Internet ..................... 347 The Web ............................... 349 HTML ................................ 350 HTML and JavaScript ....................... 354 In the sandbox ............................ 356 Compatibility and the browser wars ............... 357 14 The Document Object Model 359 Document structure ......................... 359 Trees ................................. 362 The standard ............................. 363 Moving through the tree