Eloquent JavaScript A Modern Introduction to Programming 2nd Edition Marijn Haverbeke download
Eloquent JavaScript A Modern Introduction to Programming 2nd Edition Marijn Haverbeke download
https://ebookultra.com/download/eloquent-javascript-a-modern-
introduction-to-programming-2nd-edition-marijn-haverbeke/
https://ebookultra.com/download/eloquent-javascript-a-modern-
introduction-to-programming-1st-edition-marijn-haverbeke/
https://ebookultra.com/download/eloquent-javascript-3rd-edition-
marijn-haverbeke/
https://ebookultra.com/download/javascript-programming-for-the-
absolute-beginner-the-fun-way-to-learn-programming-1st-edition-andy-
harris/
https://ebookultra.com/download/an-introduction-to-programming-in-
emacs-lisp-2nd-edition-robert-j-chassell/
Introduction to modern physics Volume 1 2nd ed Edition
Singh
https://ebookultra.com/download/introduction-to-modern-physics-
volume-1-2nd-ed-edition-singh/
https://ebookultra.com/download/pro-javascript-with-mootools-learning-
advanced-javascript-programming-1st-edition-mark-joseph-obcena-auth/
https://ebookultra.com/download/introduction-to-javascript-object-
notation-a-to-the-point-guide-to-json-1-early-release-edition-lindsay-
bassett/
https://ebookultra.com/download/an-introduction-to-programming-using-
alice-2-2-2nd-edition-charles-w-herbert/
https://ebookultra.com/download/matlab-a-practical-introduction-to-
programming-and-problem-solving-1st-edition-stormy-attaway/
Eloquent JavaScript A Modern Introduction to
Programming 2nd Edition Marijn Haverbeke Digital
Instant Download
Author(s): Marijn Haverbeke
ISBN(s): 9781593275846, 1593275846
Edition: 2
File Details: PDF, 3.15 MB
Year: 2014
Language: english
Eloquent JavaScript
A Modern Introduction to Programming
Marijn Haverbeke
Copyright © 2014 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://www.amazon.com/gp/product/
1593275846/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=
1593275846&linkCode=as2&tag=marijhaver-20&linkId=VPXXXSRYC5COG5R5.
i
Contents
On programming . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Why language matters . . . . . . . . . . . . . . . . . . . . . . . 4
What is JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . 6
Code, and what to do with it . . . . . . . . . . . . . . . . . . . 8
Typographic conventions . . . . . . . . . . . . . . . . . . . . . . 9
2 Program Structure 22
Expressions and statements . . . . . . . . . . . . . . . . . . . . 22
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Keywords and reserved words . . . . . . . . . . . . . . . . . . . 25
The environment . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The console.log function . . . . . . . . . . . . . . . . . . . . . . 27
Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
prompt and confirm . . . . . . . . . . . . . . . . . . . . . . . . . 28
Control flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . 29
while and do loops . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Indenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
ii
Breaking Out of a Loop . . . . . . . . . . . . . . . . . . . . . . 35
Updating variables succinctly . . . . . . . . . . . . . . . . . . . 35
Dispatching on a value with switch . . . . . . . . . . . . . . . . 36
Capitalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3 Functions 41
Defining a function . . . . . . . . . . . . . . . . . . . . . . . . . 41
Parameters and scopes . . . . . . . . . . . . . . . . . . . . . . . 42
Nested scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Functions as values . . . . . . . . . . . . . . . . . . . . . . . . . 45
Declaration notation . . . . . . . . . . . . . . . . . . . . . . . . 46
The call stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 48
Closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Growing functions . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Functions and side effects . . . . . . . . . . . . . . . . . . . . . 57
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
iii
The Math object . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
The global object . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5 Higher-Order Functions 85
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Abstracting array traversal . . . . . . . . . . . . . . . . . . . . . 87
Higher-order functions . . . . . . . . . . . . . . . . . . . . . . . 89
Passing along arguments . . . . . . . . . . . . . . . . . . . . . . 90
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Filtering an array . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Transforming with map . . . . . . . . . . . . . . . . . . . . . . . 94
Summarizing with reduce . . . . . . . . . . . . . . . . . . . . . . 94
Composability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The cost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Great-great-great-great-… . . . . . . . . . . . . . . . . . . . . . . 98
Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
iv
7 Project: Electronic Life 127
Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Representing space . . . . . . . . . . . . . . . . . . . . . . . . . 128
A critter’s programming interface . . . . . . . . . . . . . . . . . 130
The world object . . . . . . . . . . . . . . . . . . . . . . . . . . 131
this and its scope . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Animating life . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
It moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
More life forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
A more lifelike simulation . . . . . . . . . . . . . . . . . . . . . 140
Action handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Populating the new world . . . . . . . . . . . . . . . . . . . . . 143
Bringing it to life . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
v
Choice patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
The mechanics of matching . . . . . . . . . . . . . . . . . . . . 170
Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
The replace method . . . . . . . . . . . . . . . . . . . . . . . . . 174
Greed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Dynamically creating RegExp objects . . . . . . . . . . . . . . 177
The search method . . . . . . . . . . . . . . . . . . . . . . . . . 178
The lastIndex property . . . . . . . . . . . . . . . . . . . . . . . 178
Parsing an INI file . . . . . . . . . . . . . . . . . . . . . . . . . . 180
International characters . . . . . . . . . . . . . . . . . . . . . . . 182
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
10 Modules 186
Why modules help . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Using functions as namespaces . . . . . . . . . . . . . . . . . . . 189
Objects as interfaces . . . . . . . . . . . . . . . . . . . . . . . . 190
Detaching from the global scope . . . . . . . . . . . . . . . . . . 191
Evaluating data as code . . . . . . . . . . . . . . . . . . . . . . 192
Require . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Slow-loading modules . . . . . . . . . . . . . . . . . . . . . . . . 195
Interface design . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
vi
The Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
HTML and JavaScript . . . . . . . . . . . . . . . . . . . . . . . 223
In the sandbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Compatibility and the browser wars . . . . . . . . . . . . . . . 225
vii
Debouncing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
17 HTTP 323
The protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Browsers and HTTP . . . . . . . . . . . . . . . . . . . . . . . . 325
viii
XMLHttpRequest . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Sending a request . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Asynchronous Requests . . . . . . . . . . . . . . . . . . . . . . . 329
Fetching XML Data . . . . . . . . . . . . . . . . . . . . . . . . . 329
HTTP sandboxing . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Abstracting requests . . . . . . . . . . . . . . . . . . . . . . . . 331
Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Appreciating HTTP . . . . . . . . . . . . . . . . . . . . . . . . . 337
Security and HTTPS . . . . . . . . . . . . . . . . . . . . . . . . 337
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
ix
20 Node.js 375
Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Asynchronicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
The node command . . . . . . . . . . . . . . . . . . . . . . . . . 377
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Installing with NPM . . . . . . . . . . . . . . . . . . . . . . . . 380
The file system module . . . . . . . . . . . . . . . . . . . . . . . 381
The HTTP module . . . . . . . . . . . . . . . . . . . . . . . . . 383
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
A simple file server . . . . . . . . . . . . . . . . . . . . . . . . . 387
Error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
x
Project: A Paint Program . . . . . . . . . . . . . . . . . . . . . 443
Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Project: Skill-Sharing Website . . . . . . . . . . . . . . . . . . . 447
xi
Introduction
This is a book about getting computers to do what you want them to do.
Computers are about as common as screwdrivers today, but contain a lot
more hidden complexity, and thus are harder to operate and understand.
To many, they remain alien, slightly threatening things.
We’ve found two effective ways of bridging the communication gap be-
tween us, squishy biological organisms with a talent for social and spatial
reasoning, and the computer, unfeeling manipulator of meaningless data.
The first is to appeal to our sense of the physical world, and build in-
terfaces that mimic that world, and allow us to manipulate shapes on a
screen with our fingers. This works very well for casual machine inter-
action.
But we have not yet found a good way to use the point-and-click ap-
proach to communicate things to the computer that the designer of the
interface did not anticipate. For open-ended interfaces, such as instruct-
ing the computer to perform arbitrary tasks, we’ve had more luck with
an approach that makes use of our talent for language: teaching the
machine a language.
Human languages allow words and subsentences to be combined in
many, many ways, allowing us to say many, many different things. Com-
puter languages, though typically less grammatically flexible, follow a
1
similar principle.
Casual computing has become much more widespread in the past
twenty years, and language-based interfaces, which once were the de-
fault way in which people interacted with computers, have largely been
replaced with graphical interfaces. But they are still there, if you know
where to look. One such language, JavaScript, is built into just about
every web browser, and thus available on just about every consumer
device.
This book intends to make you familiar enough with this language to
be able to make a computer do what you want.
On programming
I do not enlighten those who are not eager to learn, nor arouse
those who are not anxious to give an explanation themselves.
If I have presented one corner of the square and they cannot
come back to me with the other three, I should not go over
the points again.
—Confucius
2
The computer programmer is a creator of universes for which
he [sic] alone is responsible. Universes of virtually unlimited
complexity can be created in the form of computer programs.
—Joseph Weizenbaum, Computer Power and Human Reason
3
Random documents with unrelated
content Scribd suggests to you:
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK
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.
ebookultra.com