Complete Download Eloquent JavaScript 4th Edition Marijn Haverbeke PDF All Chapters
Complete Download Eloquent JavaScript 4th Edition Marijn Haverbeke PDF All Chapters
com
https://ebookmass.com/product/eloquent-javascript-4th-
edition-marijn-haverbeke/
OR CLICK HERE
DOWLOAD NOW
JavaScript T. J. Crowder
https://ebookmass.com/product/javascript-t-j-crowder/
ebookmass.com
https://ebookmass.com/product/javascript-a-beginners-guide-fifth-
edition-pollock/
ebookmass.com
https://ebookmass.com/product/shigleys-mechanical-engineering-
design-11th-ed-11th-edition-richard-g-budynas/
ebookmass.com
MYP Mathematics 2 (IB MYP SERIES) 1st Edition Torres-
Skoumal
https://ebookmass.com/product/myp-mathematics-2-ib-myp-series-1st-
edition-torres-skoumal/
ebookmass.com
https://ebookmass.com/product/the-environmental-impact-of-
covid-19-deepak-rawtani/
ebookmass.com
https://ebookmass.com/product/fitzgeralds-clinical-neuroanatomy-and-
neuroscience-8th-edition-estomih-mtui/
ebookmass.com
https://ebookmass.com/product/shiism-revisited-ijtihad-and-
reformation-in-contemporary-times-liyakat-takim/
ebookmass.com
Electronic materials : principles and applied science
Poplavko
https://ebookmass.com/product/electronic-materials-principles-and-
applied-science-poplavko/
ebookmass.com
Eloquent JavaScript
4th edition
Marijn Haverbeke
Copyright © 2024 by Marijn Haverbeke
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.
i
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
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
ii
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
iii
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5 Higher-Order Functions 79
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Abstracting repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Higher-order functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Script data set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
iv
The mail truck’s route . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Pathfinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
v
10 Modules 161
Modular programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
ES modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
CommonJS modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Building and bundling . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Module design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
vi
In the sandbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Compatibility and the browser wars . . . . . . . . . . . . . . . . . . . 212
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
viii
File fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Storing data client-side . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
20 Node.js 337
Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
The node command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Installing with NPM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
The file system module . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
The HTTP module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
A file server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
ix
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Data Structures: Objects and Arrays . . . . . . . . . . . . . . . . . . . 375
Higher-Order Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
The Secret Life of Objects . . . . . . . . . . . . . . . . . . . . . . . . . 378
Project: A Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Bugs and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Asynchronous Programming . . . . . . . . . . . . . . . . . . . . . . . . 382
Project: A Programming Language . . . . . . . . . . . . . . . . . . . . 384
The Document Object Model . . . . . . . . . . . . . . . . . . . . . . . 385
Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Project: A Platform Game . . . . . . . . . . . . . . . . . . . . . . . . . 387
Drawing on Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
HTTP and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Project: A Pixel Art Editor . . . . . . . . . . . . . . . . . . . . . . . . 391
Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Project: Skill-Sharing Website . . . . . . . . . . . . . . . . . . . . . . . 394
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
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 often is no appropriate 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. On top of that, it makes for a wonderful game of puzzle solving and
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.
For routine computer use, these have largely been replaced with visual inter-
faces, which are easier to learn but offer less freedom. But if you know where
to look, the languages are still there. One of them, 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.
1
On programming
Besides explaining JavaScript, I will introduce the basic principles of program-
ming. Programming, it turns out, is hard. The fundamental rules are simple
and clear, but programs built on top of these rules tend to become complex
enough to introduce their own rules and complexity. You’re building your own
maze, in a way, and you can easily get lost in it.
There will be times when reading this book feels terribly frustrating. If you
are new to programming, there will be a lot of new material to digest. Much of
this material will then be combined in ways that require you to make additional
connections.
It is up to you to make the necessary effort. When you are struggling to
follow the book, do not jump to any conclusions about your own capabilities.
You are fine—you just need to keep at it. Take a break, reread some material,
and make sure you read and understand the example programs and exercises.
Learning is hard work, but everything you learn is yours and will make further
learning easier.
2
Some programmers believe that this complexity is best managed by using
only a small set of well-understood techniques in their programs. They have
composed strict rules (“best practices”) prescribing the form programs should
have and carefully stay within their safe little zone.
This is not only boring, it is ineffective. New problems often require new
solutions. The field of programming is young and still developing rapidly, and
it is varied enough to have room for wildly different approaches. There are
many terrible mistakes to make in program design, and you should go ahead
and make them at least once so that you understand them. A sense of what a
good program looks like is developed with practice, not learned from a list of
rules.
This is a program to add the numbers from 1 to 10 together and print the
result: 1 + 2 + ... + 10 = 55. It could run on a simple hypothetical machine.
To program early computers, it was necessary to set large arrays of switches
in the right position or punch holes in strips of cardboard and feed them to
the computer. You can imagine how tedious and error-prone this procedure
was. Even writing simple programs required much cleverness and discipline.
Complex ones were nearly inconceivable.
Of course, manually entering these arcane patterns of bits (the ones and
zeros) did give the programmer a profound sense of being a mighty wizard.
And that has to be worth something in terms of job satisfaction.
Each line of the previous program contains a single instruction. It could be
written in English like this:
3
Random documents with unrelated
content Scribd suggests to you:
XV. FEJEZET.
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com