基本信息
源码名称:JavaScript for impatient programmers ES2021 edition-2021.pdf
源码大小:2.39M
文件格式:.pdf
开发语言:js
更新时间:2021-04-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
JavaScript 最新英文原版书籍
Contents I Background 13 1 Before you buy the book 15 1.1 About the content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.2 Previewing and buying this book . . . . . . . . . . . . . . . . . . . . . . 16 1.3 About the author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.4 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 FAQ: book and supplementary material 19 2.1 How to read this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2 I own a digital version . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.3 I own the print version . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.4 Notations and conventions . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3 Why JavaScript? (bonus) 23 3.1 The cons of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.2 The pros of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.3 Pro and con of JavaScript: innovation . . . . . . . . . . . . . . . . . . . . 25 4 The nature of JavaScript (bonus) 27 4.1 JavaScript’s influences . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4.2 The nature of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4.3 Tips for getting started with JavaScript . . . . . . . . . . . . . . . . . . . 28 5 History and evolution of JavaScript 31 5.1 How JavaScript was created . . . . . . . . . . . . . . . . . . . . . . . . . 31 5.2 Standardizing JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 5.3 Timeline of ECMAScript versions . . . . . . . . . . . . . . . . . . . . . . 32 5.4 Ecma Technical Committee 39 (TC39) . . . . . . . . . . . . . . . . . . . . 33 5.5 The TC39 process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 5.6 FAQ: TC39 process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5.7 Evolving JavaScript: Don’t break the web . . . . . . . . . . . . . . . . . . 35 6 New JavaScript features 37 6.1 New in ECMAScript 2021 . . . . . . . . . . . . . . . . . . . . . . . . . . 37 6.2 New in ECMAScript 2020 . . . . . . . . . . . . . . . . . . . . . . . . . . 37 6.3 New in ECMAScript 2019 . . . . . . . . . . . . . . . . . . . . . . . . . . 38 6.4 New in ECMAScript 2018 . . . . . . . . . . . . . . . . . . . . . . . . . . 38 3 4 CONTENTS 6.5 New in ECMAScript 2017 . . . . . . . . . . . . . . . . . . . . . . . . . . 38 6.6 New in ECMAScript 2016 . . . . . . . . . . . . . . . . . . . . . . . . . . 39 6.7 Source of this chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 7 FAQ: JavaScript 41 7.1 What are good references for JavaScript? . . . . . . . . . . . . . . . . . . 41 7.2 How do I find out what JavaScript features are supported where? . . . . 41 7.3 Where can I look up what features are planned for JavaScript? . . . . . . 42 7.4 Why does JavaScript fail silently so often? . . . . . . . . . . . . . . . . . 42 7.5 Why can’t we clean up JavaScript, by removing quirks and outdated features? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 7.6 How can I quickly try out a piece of JavaScript code? . . . . . . . . . . . 42 II First steps 43 8 Using JavaScript: the big picture 45 8.1 What are you learning in this book? . . . . . . . . . . . . . . . . . . . . . 45 8.2 The structure of browsers and Node.js . . . . . . . . . . . . . . . . . . . 45 8.3 JavaScript references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 8.4 Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 9 Syntax 47 9.1 An overview of JavaScript’s syntax . . . . . . . . . . . . . . . . . . . . . 48 9.2 (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 9.3 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 9.4 Statement vs. expression . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 9.5 Ambiguous syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 9.6 Semicolons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 9.7 Automatic semicolon insertion (ASI) . . . . . . . . . . . . . . . . . . . . 60 9.8 Semicolons: best practices . . . . . . . . . . . . . . . . . . . . . . . . . . 61 9.9 Strict mode vs. sloppy mode . . . . . . . . . . . . . . . . . . . . . . . . . 62 10 Consoles: interactive JavaScript command lines 65 10.1 Trying out JavaScript code . . . . . . . . . . . . . . . . . . . . . . . . . . 65 10.2 The console.* API: printing data and more . . . . . . . . . . . . . . . . 67 11 Assertion API 71 11.1 Assertions in software development . . . . . . . . . . . . . . . . . . . . . 71 11.2 How assertions are used in this book . . . . . . . . . . . . . . . . . . . . 71 11.3 Normal comparison vs. deep comparison . . . . . . . . . . . . . . . . . . 72 11.4 Quick reference: module assert . . . . . . . . . . . . . . . . . . . . . . . 73 12 Getting started with quizzes and exercises 77 12.1 Quizzes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 12.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 12.3 Unit tests in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 CONTENTS 5 III Variables and values 83 13 Variables and assignment 85 13.1 let . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 13.2 const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 13.3 Deciding between const and let . . . . . . . . . . . . . . . . . . . . . . 87 13.4 The scope of a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 13.5 (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 13.6 Terminology: static vs. dynamic . . . . . . . . . . . . . . . . . . . . . . . 89 13.7 Global variables and the global object . . . . . . . . . . . . . . . . . . . . 90 13.8 Declarations: scope and activation . . . . . . . . . . . . . . . . . . . . . . 92 13.9 Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 14 Values 99 14.1 What’s a type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 14.2 JavaScript’s type hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . 100 14.3 The types of the language specification . . . . . . . . . . . . . . . . . . . 100 14.4 Primitive values vs. objects . . . . . . . . . . . . . . . . . . . . . . . . . . 101 14.5 The operators typeof and instanceof: what’s the type of a value? . . . . 103 14.6 Classes and constructor functions . . . . . . . . . . . . . . . . . . . . . . 105 14.7 Converting between types . . . . . . . . . . . . . . . . . . . . . . . . . . 106 15 Operators 109 15.1 Making sense of operators . . . . . . . . . . . . . . . . . . . . . . . . . . 109 15.2 The plus operator ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 15.3 Assignment operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 15.4 Equality: == vs. === . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 15.5 Ordering operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 15.6 Various other operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 IV Primitive values 117 16 The non-values undefined and null 119 16.1 undefined vs. null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 16.2 Occurrences of undefined and null . . . . . . . . . . . . . . . . . . . . . 120 16.3 Checking for undefined or null . . . . . . . . . . . . . . . . . . . . . . . 121 16.4 The nullish coalescing operator (??) for default values [ES2020] . . . . . . 121 16.5 undefined and null don’t have properties . . . . . . . . . . . . . . . . . 124 16.6 The history of undefined and null . . . . . . . . . . . . . . . . . . . . . 125 17 Booleans 127 17.1 Converting to boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 17.2 Falsy and truthy values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 17.3 Truthiness-based existence checks . . . . . . . . . . . . . . . . . . . . . . 129 17.4 Conditional operator (? :) . . . . . . . . . . . . . . . . . . . . . . . . . . 131 17.5 Binary logical operators: And (x && y), Or (x || y) . . . . . . . . . . . . 132 17.6 Logical Not (!) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 6 CONTENTS 18 Numbers 135 18.1 Numbers are used for both floating point numbers and integers . . . . . 136 18.2 Number literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 18.3 Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 18.4 Converting to number . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 18.5 Error values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 18.6 The precision of numbers: careful with decimal fractions . . . . . . . . . 144 18.7 (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 18.8 Background: floating point precision . . . . . . . . . . . . . . . . . . . . 144 18.9 Integer numbers in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . 146 18.10Bitwise operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 18.11Quick reference: numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 151 19 Math 157 19.1 Data properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 19.2 Exponents, roots, logarithms . . . . . . . . . . . . . . . . . . . . . . . . . 158 19.3 Rounding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 19.4 Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 19.5 Various other functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 19.6 Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 20 Bigints – arbitrary-precision integers [ES2020] (advanced) 165 20.1 Why bigints? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 20.2 Bigints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 20.3 Bigint literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 20.4 Reusing number operators for bigints (overloading) . . . . . . . . . . . . 168 20.5 The wrapper constructor BigInt . . . . . . . . . . . . . . . . . . . . . . . 172 20.6 Coercing bigints to other primitive types . . . . . . . . . . . . . . . . . . 174 20.7 TypedArrays and DataView operations for 64-bit values . . . . . . . . . . 174 20.8 Bigints and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 20.9 FAQ: Bigints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 21 Unicode – a brief introduction (advanced) 177 21.1 Code points vs. code units . . . . . . . . . . . . . . . . . . . . . . . . . . 177 21.2 Encodings used in web development: UTF-16 and UTF-8 . . . . . . . . . 180 21.3 Grapheme clusters – the real characters . . . . . . . . . . . . . . . . . . . 180 22 Strings 183 22.1 Plain string literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 22.2 Accessing characters and code points . . . . . . . . . . . . . . . . . . . . 184 22.3 String concatenation via . . . . . . . . . . . . . . . . . . . . . . . . . . 185 22.4 Converting to string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 22.5 Comparing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 22.6 Atoms of text: Unicode characters, JavaScript characters, grapheme clusters188 22.7 Quick reference: Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 23 Using template literals and tagged templates 199 23.1 Disambiguation: “template” . . . . . . . . . . . . . . . . . . . . . . . . . 199 23.2 Template literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 CONTENTS 7 23.3 Tagged templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 23.4 Examples of tagged templates (as provided via libraries) . . . . . . . . . 203 23.5 Raw string literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 23.6 (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 23.7 Multiline template literals and indentation . . . . . . . . . . . . . . . . . 204 23.8 Simple templating via template literals . . . . . . . . . . . . . . . . . . . 206 24 Symbols 209 24.1 Symbols are primitives that are also like objects . . . . . . . . . . . . . . 209 24.2 The descriptions of symbols . . . . . . . . . . . . . . . . . . . . . . . . . 210 24.3 Use cases for symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 24.4 Publicly known symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 24.5 Converting symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 V Control flow and data flow 217 25 Control flow statements 219 25.1 Controlling loops: break and continue . . . . . . . . . . . . . . . . . . . 220 25.2 Conditions of control flow statements . . . . . . . . . . . . . . . . . . . . 221 25.3 if statements [ES1] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 25.4 switch statements [ES3] . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 25.5 while loops [ES1] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 25.6 do-while loops [ES3] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 25.7 for loops [ES1] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 25.8 for-of loops [ES6] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 25.9 for-await-of loops [ES2018] . . . . . . . . . . . . . . . . . . . . . . . . 229 25.10for-in loops (avoid) [ES1] . . . . . . . . . . . . . . . . . . . . . . . . . . 229 25.11Recomendations for looping . . . . . . . . . . . . . . . . . . . . . . . . . 229 26 Exception handling 231 26.1 Motivation: throwing and catching exceptions . . . . . . . . . . . . . . . 231 26.2 throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 26.3 The try statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 26.4 Error classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 27 Callable values 237 27.1 Kinds of functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 27.2 Ordinary functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 27.3 Specialized functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 27.4 Summary: kinds of callable values . . . . . . . . . . . . . . . . . . . . . . 246 27.5 Returning values from functions and methods . . . . . . . . . . . . . . . 247 27.6 Parameter handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 27.7 Methods of functions: .call(), .apply(), .bind() . . . . . . . . . . . . . 252 28 Evaluating code dynamically: eval(), new Function() (advanced) 255 28.1 eval() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 28.2 new Function() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 28.3 Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 8 CONTENTS VI Modularity 259 29 Modules 261 29.1 Overview: syntax of ECMAScript modules . . . . . . . . . . . . . . . . . 262 29.2 JavaScript source code formats . . . . . . . . . . . . . . . . . . . . . . . . 263 29.3 Before we had modules, we had scripts . . . . . . . . . . . . . . . . . . . 263 29.4 Module systems created prior to ES6 . . . . . . . . . . . . . . . . . . . . 264 29.5 ECMAScript modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 29.6 Named exports and imports . . . . . . . . . . . . . . . . . . . . . . . . . 267 29.7 Default exports and imports . . . . . . . . . . . . . . . . . . . . . . . . . 269 29.8 More details on exporting and importing . . . . . . . . . . . . . . . . . . 272 29.9 npm packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 29.10Naming modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 29.11Module specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 29.12Loading modules dynamically via import() [ES2020] . . . . . . . . . . . 278 29.13import.meta – metadata for the current module [ES2020] . . . . . . . . . 280 29.14Polyfills: emulating native web platform features (advanced) . . . . . . . 282 30 Single objects 283 30.1 What is an object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 30.2 Objects as records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 30.3 Spreading into object literals (...) [ES2018] . . . . . . . . . . . . . . . . . 288 30.4 Methods and the special variable this . . . . . . . . . . . . . . . . . . . 290 30.5 Optional chaining for property accesses and method calls [ES2020] (advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 30.6 Objects as dictionaries (advanced) . . . . . . . . . . . . . . . . . . . . . . 298 30.7 Standard methods (advanced) . . . . . . . . . . . . . . . . . . . . . . . . 306 30.8 Advanced topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 31 Prototype chains and classes 311 31.1 Prototype chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 31.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 31.3 Private data for classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 31.4 Subclassing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 31.5 FAQ: objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 VII Collections 333 32 Synchronous iteration 335 32.1 What is synchronous iteration about? . . . . . . . . . . . . . . . . . . . . 335 32.2 Core iteration constructs: iterables and iterators . . . . . . . . . . . . . . 336 32.3 Iterating manually . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 32.4 Iteration in practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 32.5 Quick reference: synchronous iteration . . . . . . . . . . . . . . . . . . . 339 33 Arrays (Array) 341 33.1 The two roles of Arrays in JavaScript . . . . . . . . . . . . . . . . . . . . 342 33.2 Basic Array operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 CONTENTS 9 33.3 for-of and Arrays [ES6] . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 33.4 Array-like objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 33.5 Converting iterable and Array-like values to Arrays . . . . . . . . . . . . 347 33.6 Creating and filling Arrays with arbitrary lengths . . . . . . . . . . . . . 348 33.7 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 33.8 More Array features (advanced) . . . . . . . . . . . . . . . . . . . . . . . 350 33.9 Adding and removing elements (destructively and non-destructively) . . 353 33.10Methods: iteration and transformation (.find(), .map(), .filter(), etc.) 355 33.11.sort(): sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 33.12Quick reference: Array<T> . . . . . . . . . . . . . . . . . . . . . . . . . . 364 34 Typed Arrays: handling binary data (advanced) 373 34.1 The basics of the API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 34.2 Element types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 34.3 More information on Typed Arrays . . . . . . . . . . . . . . . . . . . . . 379 34.4 Quick references: indices vs. offsets . . . . . . . . . . . . . . . . . . . . . 382 34.5 Quick reference: ArrayBuffers . . . . . . . . . . . . . . . . . . . . . . . . 383 34.6 Quick reference: Typed Arrays . . . . . . . . . . . . . . . . . . . . . . . . 384 34.7 Quick reference: DataViews . . . . . . . . . . . . . . . . . . . . . . . . . 387 35 Maps (Map) 389 35.1 Using Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 35.2 Example: Counting characters . . . . . . . . . . . . . . . . . . . . . . . . 393 35.3 A few more details about the keys of Maps (advanced) . . . . . . . . . . 393 35.4 Missing Map operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 35.5 Quick reference: Map<K,V> . . . . . . . . . . . . . . . . . . . . . . . . . . 396 35.6 FAQ: Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 36 WeakMaps (WeakMap) (advanced) 401 36.1 WeakMaps are black boxes . . . . . . . . . . . . . . . . . . . . . . . . . . 401 36.2 The keys of a WeakMap are weakly held . . . . . . . . . . . . . . . . . . . 402 36.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 36.4 WeakMap API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 37 Sets (Set) 405 37.1 Using Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 37.2 Examples of using Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 37.3 What Set elements are considered equal? . . . . . . . . . . . . . . . . . . 407 37.4 Missing Set operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 37.5 Quick reference: Set<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 37.6 FAQ: Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 38 WeakSets (WeakSet) (advanced) 413 38.1 Example: Marking objects as safe to use with a method . . . . . . . . . . 413 38.2 WeakSet API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414 39 Destructuring 415 39.1 A first taste of destructuring . . . . . . . . . . . . . . . . . . . . . . . . . 416 39.2 Constructing vs. extracting . . . . . . . . . . . . . . . . . . . . . . . . . . 416 10 CONTENTS 39.3 Where can we destructure? . . . . . . . . . . . . . . . . . . . . . . . . . . 417 39.4 Object-destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 39.5 Array-destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 39.6 Examples of destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . 420 39.7 What happens if a pattern part does not match anything? . . . . . . . . . 422 39.8 What values can’t be destructured? . . . . . . . . . . . . . . . . . . . . . 422 39.9 (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 39.10Default values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 39.11Parameter definitions are similar to destructuring . . . . . . . . . . . . . 424 39.12Nested destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424 40 Synchronous generators (advanced) 427 40.1 What are synchronous generators? . . . . . . . . . . . . . . . . . . . . . 427 40.2 Calling generators from generators (advanced) . . . . . . . . . . . . . . . 431 40.3 Background: external iteration vs. internal iteration . . . . . . . . . . . . 433 40.4 Use case for generators: reusing traversals . . . . . . . . . . . . . . . . . 434 40.5 Advanced features of generators . . . . . . . . . . . . . . . . . . . . . . . 435 VIII Asynchronicity 437 41 Asynchronous programming in JavaScript 439 41.1 A roadmap for asynchronous programming in JavaScript . . . . . . . . . 440 41.2 The call stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442 41.3 The event loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 41.4 How to avoid blocking the JavaScript process . . . . . . . . . . . . . . . 444 41.5 Patterns for delivering asynchronous results . . . . . . . . . . . . . . . . 446 41.6 Asynchronous code: the downsides . . . . . . . . . . . . . . . . . . . . . 449 41.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 42 Promises for asynchronous programming [ES6] 451 42.1 The basics of using Promises . . . . . . . . . . . . . . . . . . . . . . . . . 452 42.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 42.3 Error handling: don’t mix rejections and exceptions . . . . . . . . . . . . 464 42.4 Promise-based functions start synchronously, settle asynchronously . . . 465 42.5 Promise combinator functions: working with Arrays of Promises . . . . . 466 42.6 Concurrency and Promise.all() (advanced) . . . . . . . . . . . . . . . . 478 42.7 Tips for chaining Promises . . . . . . . . . . . . . . . . . . . . . . . . . . 480 42.8 Quick reference: Promise combinator functions . . . . . . . . . . . . . . 482 43 Async functions 485 43.1 Async functions: the basics . . . . . . . . . . . . . . . . . . . . . . . . . . 485 43.2 Returning from async functions . . . . . . . . . . . . . . . . . . . . . . . 487 43.3 await: working with Promises . . . . . . . . . . . . . . . . . . . . . . . . 489 43.4 (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 43.5 Immediately invoked async arrow functions . . . . . . . . . . . . . . . . 491 43.6 Concurrency and await . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 43.7 Tips for using async functions . . . . . . . . . . . . . . . . . . . . . . . . 493 CONTENTS 11 44 Asynchronous iteration 495 44.1 Basic asynchronous iteration . . . . . . . . . . . . . . . . . . . . . . . . . 495 44.2 Asynchronous generators . . . . . . . . . . . . . . . . . . . . . . . . . . 498 44.3 Async iteration over Node.js streams . . . . . . . . . . . . . . . . . . . . 502 IX More standard library 505 45 Regular expressions (RegExp) 507 45.1 Creating regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . 508 45.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 45.3 Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 45.4 Properties of regular expression objects . . . . . . . . . . . . . . . . . . . 517 45.5 Methods for working with regular expressions . . . . . . . . . . . . . . . 518 45.6 The flags /g and /y, and the property .lastIndex (advanced) . . . . . . . 525 45.7 Techniques for working with regular expressions . . . . . . . . . . . . . . 535 46 Dates (Date) 537 46.1 Best practice: avoid the built-in Date . . . . . . . . . . . . . . . . . . . . 537 46.2 Time standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 46.3 Background: date time formats (ISO) . . . . . . . . . . . . . . . . . . . . 539 46.4 Time values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541 46.5 Creating Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 46.6 Getters and setters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 46.7 Converting Dates to strings . . . . . . . . . . . . . . . . . . . . . . . . . 543 47 Creating and parsing JSON (JSON) 545 47.1 The discovery and standardization of JSON . . . . . . . . . . . . . . . . 546 47.2 JSON syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546 47.3 Using the JSON API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 47.4 Customizing stringification and parsing (advanced) . . . . . . . . . . . . 549 47.5 FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 X Miscellaneous topics 555 48 Next steps: overview of web development (bonus) 557 48.1 Tips against feeling overwhelmed . . . . . . . . . . . . . . . . . . . . . . 557 48.2 Things worth learning for web development . . . . . . . . . . . . . . . . 558 48.3 Example: tool-based JavaScript workflow . . . . . . . . . . . . . . . . . . 560 48.4 An overview of JavaScript tools . . . . . . . . . . . . . . . . . . . . . . . 562 48.5 Tools not related to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . 564 XI Appendices 567 49 Index 569