Eloquent Javascript 3rd Edition

Eloquent Javascript 3rd Edition

$10.00

Introduction
This is a book about instructing computers. Computers are about as common
as screwdrivers today, but they are quite a bit more complex, and making them
do what you want them to do isn’t always easy.
If the task you have for your computer is a common, well-understood one,
such as showing you your email or acting like a calculator, you can open the
appropriate application and get to work. But for unique or open-ended tasks,
there probably is no application.
That is where programming may come in. Programming is the act of con
structing a program—a set of precise instructions telling a computer what to do.
Because computers are dumb, pedantic beasts, programming is fundamentally
tedious and frustrating.
Fortunately, if you can get over that fact, and maybe even enjoy the rigor
of thinking in terms that dumb machines can deal with, programming can be
rewarding. It allows you to do things in seconds that would take forever by
hand. It is a way to make your computer tool do things that it couldn’t do
before. And it provides a wonderful exercise in abstract thinking.
Most programming is done with programming languages. A programming
language is an artificially constructed language used to instruct computers. It
is interesting that the most effective way we’ve found to communicate with a
computer borrows so heavily from the way we communicate with each other.
Like human languages, computer languages allow words and phrases to be
combined in new ways, making it possible to express ever new concepts.
At one point language-based interfaces, such as the BASIC and DOS prompts
of the 1980s and 1990s, were the main method of interacting with computers.
They have largely been replaced with visual interfaces, which are easier to learn
but offer less freedom. Computer languages are still there, if you know where
to look. One such language, JavaScript, is built into every modern web browser
and is thus available on almost every device.
This book will try to make you familiar enough with this language to do
useful and amusing things with it.

Category:

Description

Contents
Introduction 1
Onprogramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Whylanguagematters . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
WhatisJavaScript?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Code,andwhattodowithit . . . . . . . . . . . . . . . . . . . . . . . 7
Overviewofthisbook. . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Typographicconventions . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1 Values,Types,andOperators 10
Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Unaryoperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Booleanvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Emptyvalues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Automatictypeconversion . . . . . . . . . . . . . . . . . . . . . . . . . 18
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2 ProgramStructure 22
Expressionsandstatements . . . . . . . . . . . . . . . . . . . . . . . . 22
Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Bindingnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Theenvironment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Theconsole.logfunction . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Returnvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Controlflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Conditionalexecution. . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
whileanddoloops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
IndentingCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
forloops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
BreakingOutofaLoop . . . . . . . . . . . . . . . . . . . . . . . . . . 33
ii
Updatingbindingssuccinctly . . . . . . . . . . . . . . . . . . . . . . . 34
Dispatchingonavaluewithswitch . . . . . . . . . . . . . . . . . . . . 34
Capitalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3 Functions 39
Definingafunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Bindingsandscopes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Functionsasvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Declarationnotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Arrowfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Thecallstack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
OptionalArguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Growingfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Functionsandsideeffects . . . . . . . . . . . . . . . . . . . . . . . . . 54
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4 DataStructures:ObjectsandArrays 57
Theweresquirrel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Mutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Thelycanthrope’slog . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Computingcorrelation . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Arrayloops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Thefinalanalysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Furtherarrayology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Stringsandtheirproperties . . . . . . . . . . . . . . . . . . . . . . . . 72
Restparameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
TheMathobject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
iii
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5 Higher-OrderFunctions 82
Abstraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Abstractingrepetition . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Higher-orderfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Scriptdataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Filteringarrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Transformingwithmap. . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Summarizingwithreduce. . . . . . . . . . . . . . . . . . . . . . . . . . 88
Composability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Stringsandcharactercodes . . . . . . . . . . . . . . . . . . . . . . . . 91
Recognizingtext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
6 TheSecretLifeofObjects 97
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Classnotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Overridingderivedproperties . . . . . . . . . . . . . . . . . . . . . . . 103
Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Symbols. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Theiteratorinterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Getters,setters,andstatics . . . . . . . . . . . . . . . . . . . . . . . . 110
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Theinstanceofoperator. . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7 Project:ARobot 117
Meadowfield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Thetask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Persistentdata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Themailtruck’sroute . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Pathfinding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
iv
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
8 BugsandErrors 128
Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Strictmode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Errorpropagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Cleaningupafterexceptions . . . . . . . . . . . . . . . . . . . . . . . . 136
Selectivecatching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
9 RegularExpressions 143
Creatingaregularexpression . . . . . . . . . . . . . . . . . . . . . . . 143
Testingformatches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Setsofcharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Repeatingpartsofapattern. . . . . . . . . . . . . . . . . . . . . . . . 146
Groupingsubexpressions . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Matchesandgroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
TheDateclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Wordandstringboundaries . . . . . . . . . . . . . . . . . . . . . . . . 150
Choicepatterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Themechanicsofmatching . . . . . . . . . . . . . . . . . . . . . . . . 151
Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Thereplacemethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Greed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
DynamicallycreatingRegExpobjects . . . . . . . . . . . . . . . . . . 157
Thesearchmethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
ThelastIndexproperty . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
ParsinganINIfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Internationalcharacters. . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
10Modules 167
Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
v
Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Improvisedmodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Evaluatingdataascode . . . . . . . . . . . . . . . . . . . . . . . . . . 170
CommonJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
ECMAScriptmodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Buildingandbundling . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Moduledesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
11AsynchronousProgramming 180
Asynchronicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Crowtech. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Networksarehard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Collectionsofpromises . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Networkflooding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Messagerouting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Asyncfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Theeventloop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Asynchronousbugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
12Project:AProgrammingLanguage 202
Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Theevaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Special forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Theenvironment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Cheating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
13JavaScriptandtheBrowser 216
NetworksandtheInternet . . . . . . . . . . . . . . . . . . . . . . . . . 216
TheWeb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
vi
HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
HTMLandJavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Inthesandbox. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Compatibilityandthebrowserwars . . . . . . . . . . . . . . . . . . . 222
14TheDocumentObjectModel 224
Documentstructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Thestandard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Movingthroughthetree . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Findingelements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Changingthedocument . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Creatingnodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Cascadingstyles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Queryselectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Positioningandanimating . . . . . . . . . . . . . . . . . . . . . . . . . 238
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
15HandlingEvents 243
Eventhandlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
EventsandDOMnodes . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Eventobjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Defaultactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Keyevents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Pointerevents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Scrollevents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Focusevents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Loadevent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Eventsandtheeventloop . . . . . . . . . . . . . . . . . . . . . . . . . 255
Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Debouncing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
vii
16Project:APlatformGame 261
Thegame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Thetechnology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Readingalevel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Encapsulationasaburden . . . . . . . . . . . . . . . . . . . . . . . . . 268
Drawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Motionandcollision. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Actorupdates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Trackingkeys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Runningthegame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
17DrawingonCanvas 284
SVG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Thecanvaselement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Linesandsurfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Drawingapiechart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Storingandclearingtransformations . . . . . . . . . . . . . . . . . . . 297
Backtothegame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Choosingagraphicsinterface . . . . . . . . . . . . . . . . . . . . . . . 304
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
18HTTPandForms 308
Theprotocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
BrowsersandHTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Fetch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
HTTPsandboxing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
AppreciatingHTTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
SecurityandHTTPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Formfields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Disabledfields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
viii
Theformasawhole . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Textfields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Checkboxesandradiobuttons . . . . . . . . . . . . . . . . . . . . . . . 321
Selectfields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Filefields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Storingdataclient-side . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
19Project:APixelArtEditor 330
Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Thestate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
DOMbuilding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Thecanvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Theapplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Drawingtools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Savingandloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Undohistory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Let’sdraw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Whyisthissohard? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
20Node.js 350
Background. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Thenodecommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
InstallingwithNPM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Thefilesystemmodule . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
TheHTTPmodule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Afileserver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
21Project: Skill-SharingWebsite 369
Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Longpolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
HTTPinterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Theserver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Theclient. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
ix
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
ExerciseHints 388
ProgramStructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
DataStructures:ObjectsandArrays . . . . . . . . . . . . . . . . . . . 390
Higher-OrderFunctions. . . . . . . . . . . . . . . . . . . . . . . . . . . 392
TheSecretLifeofObjects . . . . . . . . . . . . . . . . . . . . . . . . . 393
Project:ARobot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
BugsandErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
RegularExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
AsynchronousProgramming . . . . . . . . . . . . . . . . . . . . . . . . 398
Project:AProgrammingLanguage . . . . . . . . . . . . . . . . . . . . 399
TheDocumentObjectModel . . . . . . . . . . . . . . . . . . . . . . . 400
HandlingEvents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Project:APlatformGame . . . . . . . . . . . . . . . . . . . . . . . . . 402
DrawingonCanvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
HTTPandForms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Project:APixelArtEditor . . . . . . . . . . . . . . . . . . . . . . . . 406
Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Project: Skill-SharingWebsite . . . . . . . . . . . . . . . . . . . . . . . 409
x
“We think we are creating the system for our own purposes. We
believe we are making it in our own image… But the computer is
not really like us. It is a projection of a very slim part of ourselves:
that portion devoted to logic, order, rule, and clarity.”
—Ellen Ullman, Close to the Machine: Technophilia and its
Discontents

Reviews

There are no reviews yet.

Be the first to review “Eloquent Javascript 3rd Edition”

Your email address will not be published. Required fields are marked *

Skip to content