0% found this document useful (0 votes)
118 views10 pages

Eloquent JavaScript, 4th Edition (A Modern Introduction To Programming) Haverbeke

Eloquent JavaScript, 4th Edition by Marijn Haverbeke is a comprehensive introduction to programming using JavaScript. The book covers fundamental concepts, programming structures, and advanced topics, making it suitable for both beginners and experienced developers. It is available for download on YakiBooki.com and is licensed under Creative Commons and MIT licenses.

Uploaded by

zuiike1975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views10 pages

Eloquent JavaScript, 4th Edition (A Modern Introduction To Programming) Haverbeke

Eloquent JavaScript, 4th Edition by Marijn Haverbeke is a comprehensive introduction to programming using JavaScript. The book covers fundamental concepts, programming structures, and advanced topics, making it suitable for both beginners and experienced developers. It is available for download on YakiBooki.com and is licensed under Creative Commons and MIT licenses.

Uploaded by

zuiike1975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program

Eloquent JavaScript
4th edition

Marijn Haverbeke

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

Copyright © 2024 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
//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program
book may also be considered licensed under an MIT license (https://eloquentjavascript.
net/code/LICENSE).
The illustrations are contributed by various artists: Cover by Péchane Sumi-
e. 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.com by Je Avallone. Game concept for Chapter 16
by Thomas Palef.

You can buy a print version of this book, with an extra bonus chapter included,
printed by No Starch Press at http://a-fwd.com/com=marijhaver-20&asin-
com=1593279507.

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program

Contents
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


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 ow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
while and do loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Indenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Breaking Out of a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 32

ii

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

Updating bindings succinctly . . . . . . . . . . . . . . . . . . . . . . . 32


Dispatching on a value with switch . . . . . . . . . . . . . . . . . . . . 33
Capitalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

3 Functions 38
De ning 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 e ects . . . . . . . . . . . . . . . . . . . . . . . . . 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 nal analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Further arrayology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Strings and their properties . . . . . . . . . . . . . . . . . . . . . . . . 69
Rest parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
The Math object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Optional property access . . . . . . . . . . . . . . . . . . . . . . . . . . 74

iii

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program
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


Meadow eld . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
The task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Persistent data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

iv

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

The mail truck’s route . . . . . . . . . . . . . . . . . . . . . . . . . . . 118


Path nding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program
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 le . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Code units and characters . . . . . . . . . . . . . . . . . . . . . . . . . 157
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

10 Modules 161
Modular programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
ES modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program
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

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

In the sandbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212


Compatibility and the browser wars . . . . . . . . . . . . . . . . . . . 212
//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program
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

This Book is Available on YakiBooki.com


Eloquent JavaScript, 4th Edition (A Modern Introduction to Programming) Haverbeke

Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Reading a level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
//www.yakibooki.com/download/eloquent-javascript-4th-edition-a-modern-introduction-to-program
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 elds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Disabled elds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
The form as a whole . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Text elds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Checkboxes and radio buttons . . . . . . . . . . . . . . . . . . . . . . . 309
Select elds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

viii

This Book is Available on YakiBooki.com

You might also like