55
66This is a book about getting ((computer))s to do what you want them to
77do. Computers are about as common as screwdrivers today, but they contain a
8- lot more hidden complexity, and thus are harder to operate and
8+ lot more hidden complexity and thus are harder to operate and
99understand. To many, they remain alien, slightly threatening things.
1010
1111image::img/generated/computer.png[alt="Communicating with a computer"]
1212
1313(((graphical user interface)))We've found two effective ways of
1414bridging the communication gap between us, squishy biological
15- organisms with a talent for social and spatial reasoning, and the
16- computer , unfeeling manipulator of meaningless data. The first is to
17- appeal to our sense of the physical world, and build interfaces that
15+ organisms with a talent for social and spatial reasoning, and
16+ computers , unfeeling manipulators of meaningless data. The first is to
17+ appeal to our sense of the physical world and build interfaces that
1818mimic that world and allow us to manipulate shapes on a screen with
1919our fingers. This works very well for casual machine interaction.
2020
@@ -26,16 +26,16 @@ tasks, we've had more luck with an approach that makes use of our
2626talent for language: teaching the machine a language.
2727
2828(((human language)))(((expressivity)))Human languages allow words and
29- phrases to be combined in many, many ways, allowing us to say
30- many, many different things. Computer languages, though typically less
29+ phrases to be combined in many ways, allowing us to say
30+ many different things. Computer languages, though typically less
3131grammatically flexible, follow a similar principle.
3232
3333(((JavaScript,availability of)))(((casual computing)))Casual computing
34- has become much more widespread in the past twenty years, and
34+ has become much more widespread in the past 20 years, and
3535language-based interfaces, which once were the default way in which
3636people interacted with computers, have largely been replaced with
3737graphical interfaces. But they are still there, if you know where to
38- look. One such language, _JavaScript_ , is built into just about every
38+ look. One such language, JavaScript , is built into just about every
3939web ((browser)) and is thus available on just about every consumer
4040device.
4141
@@ -55,7 +55,7 @@ points again.
5555____
5656
5757(((programming,difficulty of)))Besides explaining JavaScript, I also
58- want to introduce the basic principles of programming. Programming, it
58+ will introduce the basic principles of programming. Programming, it
5959turns out, is hard. The fundamental rules are typically simple and
6060clear. But programs built on top of these rules tend to become complex
6161enough to introduce their own rules and complexity. You're building
@@ -69,9 +69,9 @@ ways that require you to make additional connections.
6969It is up to you to make the effort necessary. When you are struggling
7070to follow the book, do not jump to any conclusions about your own
7171capabilities. You are fine—you just need to keep at it. Take a break,
72- re-read some material, and _always_ make sure you read and understand
72+ reread some material, and _always_ make sure you read and understand
7373the example programs and ((exercises)). Learning is hard work, but
74- everything you learn is yours, and will make subsequent learning
74+ everything you learn is yours and will make subsequent learning
7575easier.
7676
7777[quote, Joseph Weizenbaum, Computer Power and Human Reason]
@@ -88,11 +88,11 @@ memory, yet it controls the actions performed on this same memory.
8888Analogies that try to compare programs to objects we are familiar with
8989tend to fall short. A superficially fitting one is that of a
9090machine—lots of separate parts tend to be involved, and to make the
91- whole thing tick we have to consider the ways in which these parts
91+ whole thing tick, we have to consider the ways in which these parts
9292interconnect and contribute to the operation of the whole.
9393
9494(((computer)))A computer is a machine built to act as a host for these
95- immaterial machines. Computers themselves can only do stupidly
95+ immaterial machines. Computers themselves can do only stupidly
9696straightforward things. The reason they are so useful is that they do
9797these things at an incredibly high speed. A program can ingeniously
9898combine enormous numbers of these simple actions in order to do very
@@ -114,7 +114,7 @@ its complexity.
114114that this complexity is best managed by using only a small set of
115115well-understood techniques in their programs. They have composed
116116strict rules (_“best practices”_) prescribing the form programs should
117- have, and the more zealous among them will consider those that go
117+ have, and the more zealous among them will consider those who go
118118outside of this safe little zone to be _bad_ programmers.
119119
120120(((experiment)))(((learning)))What hostility to the richness of
@@ -149,9 +149,9 @@ languages. Programs looked something like this:
149149
150150(((programming,history of)))(((punch card)))(((complexity)))That is a
151151program to add the numbers from 1 to 10 together and print out the
152- result `( 1 + 2 + ... + 10 = 55) `. It could run on a very simple,
152+ result: ` 1 + 2 + ... + 10 = 55`. It could run on a simple,
153153hypothetical machine. To program early computers, it was necessary to
154- set large arrays of switches in the right position, or punch holes in
154+ set large arrays of switches in the right position or punch holes in
155155strips of cardboard and feed them to the computer. You can probably imagine how
156156how tedious and error-prone this procedure was. Even writing simple
157157programs required much cleverness and discipline. Complex ones were
@@ -162,7 +162,7 @@ arcane patterns of bits (the ones and zeros) did give the programmer
162162a profound sense of being a mighty wizard. And that has to be worth
163163something in terms of job satisfaction.
164164
165- (((memory)))(((instruction)))Each line of the program above contains a
165+ (((memory)))(((instruction)))Each line of the previous program contains a
166166single instruction. It could be written in English like this:
167167
168168[source,text/plain]
@@ -182,21 +182,21 @@ single instruction. It could be written in English like this:
182182(((readability)))(((naming)))(((variable)))Although that is already
183183more readable than the soup of bits, it is still rather unpleasant. It
184184might help to use names instead of numbers for the instructions and
185- memory locations:
185+ memory locations.
186186
187187[source,text/plain]
188188----
189- Set “total” to 0
190- Set “count” to 1
189+ Set “total” to 0.
190+ Set “count” to 1.
191191[loop]
192- Set “compare” to “count”
193- Subtract 11 from “compare”
194- If “compare” is zero, continue at [end]
195- Add “count” to “total”
196- Add 1 to “count”
197- Continue at [loop]
192+ Set “compare” to “count”.
193+ Subtract 11 from “compare”.
194+ If “compare” is zero, continue at [end].
195+ Add “count” to “total”.
196+ Add 1 to “count”.
197+ Continue at [loop].
198198[end]
199- Output “total”
199+ Output “total”.
200200----
201201
202202(((loop)))(((jump)))(((summing example)))Can you see how the program
@@ -205,7 +205,7 @@ two memory locations their starting values: `total` will be used to
205205build up the result of the computation, and `count` will keep track of the
206206number that we are currently looking at. The lines using `compare` are
207207probably the weirdest ones. The program wants to see
208- whether `count` is equal to 11 in order to decide if it can stop
208+ whether `count` is equal to 11 in order to decide whether it can stop
209209running. Because our hypothetical machine is rather primitive, it can only
210210test whether a number is zero and make a decision (or jump) based on
211211that. So, it uses the memory location labeled `compare` to compute the
@@ -228,7 +228,7 @@ console.log(total);
228228
229229(((while loop)))(((loop)))This version gives us a few more improvements.
230230Most importantly, there is no need to specify the way we want the
231- program to jump back and forth any more . The `while` language
231+ program to jump back and forth anymore . The `while` language
232232construct takes care of that. It continues executing the block
233233(wrapped in braces) below it as long as the condition it was given
234234holds. That condition is `count <= 10`, which means “++count++ is less than or equal to
@@ -275,8 +275,8 @@ easy to compose.
275275
276276indexsee:[WWW,World Wide Web] indexsee:[Web,World Wide Web](((history)))(((Netscape)))(((browser)))(((web
277277application)))(((JavaScript)))(((JavaScript,history of)))(((World Wide
278- Web))) JavaScript was introduced in 1995, as a way to add programs to
279- Web pages in the Netscape Navigator browser. The language has since
278+ Web))) JavaScript was introduced in 1995 as a way to add programs to
279+ web pages in the Netscape Navigator browser. The language has since
280280been adopted by all other major graphical web browsers. It has made modern
281281web applications possible, applications with which you can interact
282282directly, without doing a page reload for every action. But it is also used in more
@@ -293,10 +293,10 @@ are stuck with the name.
293293
294294(((ECMAScript)))(((compatibility)))After its adoption outside of
295295Netscape, a ((standard)) document was written to describe the way the
296- JavaScript language should work, to make sure the various pieces of
296+ JavaScript language should work to make sure the various pieces of
297297software that claimed to support JavaScript were actually talking
298298about the same language. This is called the ECMAScript standard, after
299- the ECMA organization which did the standardization. In practice, the
299+ the ECMA organization that did the standardization. In practice, the
300300terms ECMAScript and JavaScript can be used interchangeably—they are
301301two names for the same language.
302302
@@ -310,13 +310,13 @@ did not have a clue what I was doing, of course, but there is a real
310310issue here: JavaScript is ridiculously liberal in what it allows. The
311311idea behind this design was that it would make programming in
312312JavaScript easier for beginners. In actuality, it mostly makes finding
313- problems in your programs harder, because the system will not point
313+ problems in your programs harder because the system will not point
314314them out to you.
315315
316316(((JavaScript,flexibility of)))(((flexibility)))This flexibility also
317317has its advantages, though. It leaves space for a lot of techniques
318- that are impossible in more rigid languages, and, as we will see, for
319- example, in the link:10_modules.html#modules[chapter on modules ], it
318+ that are impossible in more rigid languages, and as you will see, for
319+ example, in link:10_modules.html#modules[Chapter 10 ], it
320320can be used to overcome some of JavaScript's shortcomings. After
321321((learning)) the language properly and working with it for a while, I have
322322learned to actually _like_ JavaScript.
@@ -328,11 +328,11 @@ JavaScript's ascent to dominance, roughly between 2000 and 2010.
328328During this time, work was underway on an ambitious version 4, which
329329planned a number of radical improvements and extensions to the
330330language. Changing a living, widely used language in such a radical
331- way turned out to be politically difficult, and work on the 4th
332- edition was abandoned in 2008, leading to the much less ambitious 5th
331+ way turned out to be politically difficult, and work on the fourth
332+ edition was abandoned in 2008, leading to the much less ambitious fifth
333333edition coming out in 2009. We're now at the point where all major
334- browsers support this 5th edition, which is the language version that
335- this book will be focusing on. A 6th edition is in the process of
334+ browsers support this fifth edition, which is the language version that
335+ this book will be focusing on. A sixth edition is in the process of
336336being finalized, and some browsers are starting to support new
337337features from this edition.
338338
@@ -348,22 +348,22 @@ outside of the browser.
348348
349349(((reading code)))(((writing code)))Code is the text that makes up
350350programs. Most chapters in this book contain quite a lot of it. In my
351- experience, reading and writing ((code)) is an indispensable part of
351+ experience, reading code and writing ((code)) are indispensable parts of
352352((learning)) to program, so try to not just glance over the examples. Read
353353them attentively and understand them. This may be slow and confusing
354354at first, but I promise that you will quickly get the hang of it. The
355355same goes for the ((exercises)). Don't assume you understand them
356356until you've actually written a working solution.
357357
358- (((interpretation)))I recommend you try out your solutions to exercises
358+ (((interpretation)))I recommend you try your solutions to exercises
359359in an actual JavaScript interpreter. That way, you'll get immediate feedback on
360- whether what you are doing is working or not , and, hopefully , you'll be
360+ whether what you are doing is working, and, I hope , you'll be
361361tempted to ((experiment)) and go beyond the exercises.
362362
363363ifdef::interactive_target[]
364364
365365When reading this book in your browser, you can edit (and run) all
366- example programs by clicking on them.
366+ example programs by clicking them.
367367
368368endif::interactive_target[]
369369
@@ -373,19 +373,19 @@ ifdef::book_target[]
373373the example code in the book, and to experiment with it, is to look it
374374up in the online version of the book at
375375http://eloquentjavascript.net/[_eloquentjavascript.net_]. There you
376- can click on any code example to edit and run it, and to see the
376+ can click any code example to edit and run it and to see the
377377output it produces. To work on the exercises, go to
378378http://eloquentjavascript.net/code[_eloquentjavascript.net/code_],
379- which provides starting code for each coding exercise, and allows you
379+ which provides starting code for each coding exercise and allows you
380380to look at the solutions.
381381
382382endif::book_target[]
383383
384384(((developer tools)))(((JavaScript console)))If you want to run the
385385programs defined in this book outside of the book's sandbox, some care
386- is required. Many examples stand on their own, and should work in any
386+ is required. Many examples stand on their own and should work in any
387387JavaScript environment. But code in later chapters is mostly written
388- for a specific environment (the browser or Node.js), and can only run
388+ for a specific environment (the browser or Node.js) and can run only
389389there. In addition, many chapters define bigger programs, and the
390390pieces of code that appear in them depend on each other or on external
391391files. The http://eloquentjavascript.net/code[sandbox] on the website
@@ -394,15 +394,15 @@ files necessary to run the code for a given chapter.
394394
395395== Overview of this book ==
396396
397- Roughly, this book contains three parts. The first 11 chapters discuss
398- the JavaScript language itself. The next 8 chapters are about web
397+ This book contains roughly three parts. The first 11 chapters discuss
398+ the JavaScript language itself. The next eight chapters are about web
399399((browsers)) and the way JavaScript is used to program them. Finally,
400- 2 chapters are devoted to ((Node.js)), another environment to program
400+ two chapters are devoted to ((Node.js)), another environment to program
401401JavaScript in.
402402
403403Throughout the book, there are five _project chapters_, which describe
404404larger example programs to give you a taste of real programming. In
405- order of appearance, we will work through an
405+ order of appearance, we will work through building an
406406link:07_elife.html[artificial life simulation], a
407407link:11_language.html[programming language], a
408408link:15_game.html#game[platform game], a
@@ -418,7 +418,7 @@ operations), and link:04_data.html#data[data structures]. After these,
418418you will be able to write simple programs. Next, Chapters
419419link:05_higher_order.html#higher_order[5] and
420420link:06_object.html#object[6] introduce techniques to use functions
421- and objects to write more _abstract_ code, and thus keep complexity
421+ and objects to write more _abstract_ code and thus keep complexity
422422under control.
423423
424424After a link:07_elife.html#elife[first project chapter], the first
@@ -435,7 +435,7 @@ link:19_paint.html#paint[19], describes the tools that browser
435435JavaScript has access to. You'll learn to display things on the screen
436436(Chapters link:13_dom.html#dom[13] and
437437link:16_canvas.html#canvas[16]), respond to user input (Chapters
438- link:14_event.html#event[14] and link:18_forms.html#forms[18]) and
438+ link:14_event.html#event[14] and link:18_forms.html#forms[18]), and
439439communicate over the network (link:17_http.html#http[Chapter 17]).
440440There are again two project chapters in this part.
441441
@@ -470,8 +470,8 @@ function fac(n) {
470470----
471471
472472(((console.log)))Sometimes, in order to show the output that a program
473- produces, the expected output is written below it, with two slashes
474- and an arrow in front:
473+ produces, the expected output is written after it, with two slashes
474+ and an arrow in front.
475475
476476[source,javascript]
477477----
0 commit comments