Skip to main content

Contents

0 -Introduction 1

  • On programming 2
  • Why language matters 3
  • What is JavaScript? 5
  • Code, and what to do with it 7
  • Overview of this book 8
  • Typographic conventions 8

1 - Values, Types, and Operators 10


https://youtu.be/Vdbh7DHoObE?list=PL-uROEx3vAxi4NyQ_8Euc1oPBfG2VJRvQ

  • Values 10
  • Numbers 11
  • Strings 13
  • Unary operators 15
  • Boolean values 15
  • Empty values 17
  • Automatic type conversion 18
  • Summary 20

2 Program Structure 21

  • Expressions and statements 21
  • Bindings 22
  • Binding names 24
  • The environment 24
  • Functions 24
  • The console.log function 25
  • Return values 25
  • Control flow 26
  • Conditional execution 26
  • while and do loops 28
  • Indenting Code 30
  • for loops 31
  • Breaking Out of a Loop 32
  • Updating bindings succinctly 32
  • Dispatching on a value with switch 33
  • Capitalization 34
  • Comments 34
  • Summary 35
  • Exercises 35

3 Functions 38

Defining a function 38

Bindings and scopes 39

Nested scope 40

Functions as values 41

Declaration notation 42

Arrow functions 42

The call stack 43

Optional Arguments 44

Closure 45

Recursion 47

Growing functions 50

Functions and side effects 52

Summary 53

Exercises 53

4 Data Structures: Objects and Arrays 55

The weresquirrel 55

Datasets 56

Properties 57

Methods 57

Objects 58

Mutability 61

The lycanthrope’s log 62

Computing correlation 64

Array loops 65

The final analysis 66

Further arrayology 68

Strings and their properties 69

Rest parameters 71

The Math object 72

Destructuring 73

Optional property access 74

iii

JSON 75

Summary 76

Exercises 76

5 Higher-Order Functions 79

Abstraction 80

Abstracting repetition 80

Higher-order functions 82

Script dataset 83

Filtering arrays 84

Transforming with map 85

Summarizing with reduce 85

Composability 86

Strings and character codes 88

Recognizing text 90

Summary 91

Exercises 91

6 The Secret Life of Objects 93

Abstract Data Types 93

Methods 94

Prototypes 95

Classes 97

Private Properties 99

Overriding derived properties 100

Maps 101

Polymorphism 102

Getters, setters, and statics 103

Symbols 105

The iterator interface 106

Inheritance 108

The instanceof operator 109

Summary 110

Exercises 111

7 Project: A Robot 112

Meadowfield 112

The task 114

Persistent data 116

Simulation 116

iv

The mail truck’s route 118

Pathfinding 119

Exercises 121

8 Bugs and Errors 123

Language 123

Strict mode 124

Types 125

Testing 126

Debugging 127

Error propagation 128

Exceptions 130

Cleaning up after exceptions 131

Selective catching 133

Assertions 135

Summary 136

Exercises 136

9 Regular Expressions 138

Creating a regular expression 138

Testing for matches 139

Sets of characters 139

International characters 140

Repeating parts of a pattern 142

Grouping subexpressions 143

Matches and groups 143

The Date class 144

Boundaries and look-ahead 145

Choice patterns 146

The mechanics of matching 147

Backtracking 147

The replace method 149

Greed 150

Dynamically creating RegExp objects 152

The search method 152

The lastIndex property 153

Parsing an INI file 154

Code units and characters 157

Summary 157

Exercises 159

v

10 Modules 161

Modular programs 161

ES modules 162

Packages 164

CommonJS modules 165

Building and bundling 168

Module design 169

Summary 171

Exercises 171

11 Asynchronous Programming 173

Asynchronicity 173

Callbacks 175

Promises 176

Failure 178

Carla 180

Breaking In 181

Async functions 182

Generators 184

A Corvid Art Project 185

The event loop 188

Asynchronous bugs 189

Summary 191

Exercises 191

12 Project: A Programming Language 193

Parsing 193

The evaluator 197

Special forms 199

The environment 200

Functions 202

Compilation 203

Cheating 203

Exercises 204

13 JavaScript and the Browser 206

Networks and the Internet 206

The Web 208

HTML 208

HTML and JavaScript 211

vi

In the sandbox 212

Compatibility and the browser wars 212

14 The Document Object Model 214

Document structure 214

Trees 215

The standard 216

Moving through the tree 217

Finding elements 218

Changing the document 219

Creating nodes 220

Attributes 222

Layout 222

Styling 224

Cascading styles 226

Query selectors 227

Positioning and animating 228

Summary 230

Exercises 230

15 Handling Events 233

Event handlers 233

Events and DOM nodes 234

Event objects 235

Propagation 235

Default actions 237

Key events 237

Pointer events 239

Scroll events 243

Focus events 244

Load event 245

Events and the event loop 245

Timers 246

Debouncing 247

Summary 248

Exercises 249

16 Project: A Platform Game 251

The game 251

The technology 252

vii

Levels 252

Reading a level 253

Actors 255

Drawing 258

Motion and collision 263

Actor updates 266

Tracking keys 268

Running the game 269

Exercises 271

17 Drawing on Canvas 273

SVG 273

The canvas element 274

Lines and surfaces 275

Paths 276

Curves 277

Drawing a pie chart 280

Text 281

Images 282

Transformation 283

Storing and clearing transformations 286

Back to the game 287

Choosing a graphics interface 292

Summary 293

Exercises 294

18 HTTP and Forms 296

The protocol 296

Browsers and HTTP 298

Fetch 299

HTTP sandboxing 301

Appreciating HTTP 301

Security and HTTPS 302

Form fields 303

Focus 304

Disabled fields 305

The form as a whole 306

Text fields 307

Checkboxes and radio buttons 309

Select fields 310

viii

File fields 311

Storing data client-side 312

Summary 315

Exercises 315

19 Project: A Pixel Art Editor 318

Components 318

The state 320

DOM building 321

The canvas 322

The application 325

Drawing tools 327

Saving and loading 330

Undo history 333

Let’s draw 334

Why is this so hard? 335

Exercises 336

20 Node.js 338

Background 338

The node command 339

Modules 340

Installing with NPM 341

The filesystem module 343

The HTTP module 344

Streams 346

A file server 347

Summary 352

Exercises 353

21 Project: Skill-Sharing Website 355

Design 355

Long polling 356

HTTP interface 357

The server 359

The client 366

Exercises 372

Exercise Hints 374

Program Structure 374

ix

Functions 375

Data Structures: Objects and Arrays 376

Higher-Order Functions 378

The Secret Life of Objects 379

Project: A Robot 380

Bugs and Errors 381

Regular Expressions 381

Modules 382

Asynchronous Programming 383

Project: A Programming Language 385

The Document Object Model 386

Handling Events 387

Project: A Platform Game 388

Drawing on Canvas 389

HTTP and Forms 391

Project: A Pixel Art Editor 392

Node.js 394

Project: Skill-Sharing Website 395