-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path00_intro.txt
202 lines (164 loc) · 8.94 KB
/
00_intro.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
:next_link: 00_environment
:load_files: ["code/intro.js"]
= Introduction =
This is a book about getting ((computer))s to do what you want them to
do. Computers are about as common as screwdrivers today, but they contain a
lot more hidden complexity and thus are harder to operate and
understand. To many, they remain alien, slightly threatening things.
image::img/generated/computer.png[alt="Communicating with a computer"]
(((Graphical user interface)))We've found two effective ways of
bridging the communication gap between us, squishy biological
organisms with a talent for social and spatial reasoning, and
computers, unfeeling manipulators of meaningless data. The first is to
appeal to our sense of the physical world and build interfaces that
mimic that world and allow us to manipulate shapes on a screen with
our fingers. This works very well for casual machine interaction.
(((programming language)))But we have not yet found a good way to use
the point-and-click approach to communicate things to the computer
that the designer of the interface did not anticipate. For open-ended
interfaces, such as instructing 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 language)))(((expressivity)))Human languages allow words and
phrases to be combined in many ways, which allows us to say
many different things. Computer languages, though typically less
grammatically flexible, follow a similar principle.
(((JavaScript,availability of)))(((casual computing)))Casual computing
has become much more widespread in the past 20 years, and
language-based interfaces, which once were the default 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 almost every
web ((browser)) and is thus available on just about every consumer
device.
indexsee:[web browser,browser]This book intends to make you familiar
enough with this language to be able to make a computer do what you
want.
== On programming ==
(((programming,difficulty of)))Besides explaining JavaScript, we'll
also introduce the basic principles of programming. Programming, it
turns out, is hard. The fundamental rules are typically 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 might just get lost in it.
(((learning)))There will be times when reading this book will feel 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 _always_ make sure you read and understand
the example programs and ((exercises)). Learning is hard work, but
everything you learn is yours and will make subsequent learning
easier.
(((program,nature of)))(((data)))A program is many things. It is a
piece of text typed by a programmer, it is the directing force that
makes the computer do what it does, it is data in the computer's
memory, yet it controls the actions performed on this same memory.
Analogies that try to compare programs to objects we are familiar with
tend to fall short. A superficially fitting one is that of a
machine—lots of separate parts tend to be involved, and to make the
whole thing tick, we have to consider the ways in which these parts
interconnect and contribute to the operation of the whole.
(((computer)))A computer is a machine built to act as a host for these
immaterial machines. Computers themselves can do only incredibly
straightforward things. The reason they are so useful is that they do
these things at an incredibly high speed. A program can ingeniously
combine an enormous number of these simple actions in order to do very
complicated things.
(((programming,joy of)))To some of us, writing computer programs is a
fascinating game. A program is a building of thought. It is costless
to build, it is weightless, and it grows easily under our typing
hands.
But without care, a program's size and ((complexity)) will grow out of
control, confusing even the person who created it. Keeping programs
under control is the main problem of programming. When a program
works, it is beautiful. The art of programming is the skill of
controlling complexity. The great program is subdued—made simple in
its complexity.
(((programming style)))(((best practices)))Many 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 the more zealous among them will consider those who go
outside of this safe little zone to be _bad_ programmers.
(((experiment)))(((learning)))What hostility to the richness of
programming—to try to reduce it to something straightforward and
predictable, to place a taboo on all the weird and beautiful programs!
The landscape of programming techniques is enormous, fascinating in
its diversity, and still largely unexplored. It is certainly dangerous
going, luring the inexperienced programmer into all kinds of
confusion, but that only means you should proceed with caution and
keep your wits about you. As you learn there will always be new
challenges and new territory to explore. Programmers who refuse to
keep exploring will stagnate, forget their joy, and get bored with
their craft.
== What is JavaScript? ==
indexsee:[WWW,World Wide Web] indexsee:[Web,World Wide Web](((history)))(((Netscape)))(((browser)))(((web
application)))(((JavaScript)))(((JavaScript,history of)))(((World Wide
Web))) JavaScript was introduced in 1995 as a way to add programs to
web pages in the Netscape Navigator browser. The language has since
been adopted by all other major graphical web browsers. It has made modern
web applications possible—applications with which you can interact
directly, without doing a page reload for every action. But it is also used in more
traditional websites to provide various forms of interactivity and
cleverness.
(((Java)))(((naming)))It is important to note that JavaScript has
almost nothing to do with the programming language named Java. The
similar name was inspired by silly marketing considerations, rather than
good judgment. When JavaScript was being introduced, the Java language
was being heavily marketed and was gaining popularity. Someone
thought it was a good idea to try to ride along on this success. Now we
are stuck with the name.
== Code, and what to do with it ==
(((reading code)))(((writing code)))Code is the text that makes up
programs. Most chapters in this book contain quite a lot of it. In my
experience, reading code and writing ((code)) are indispensable parts of
((learning)) to program, so try to not just glance over the examples. Read
them attentively and understand them. This may be slow and confusing
at first, but I promise that you will quickly get the hang of it. The
same goes for the ((exercises)). Don't assume you understand them
until you've actually written a working solution.
(((interpretation)))We recommend you try your solutions to exercises
in an actual JavaScript interpreter. That way, you'll get immediate feedback on
whether what you are doing is working, and, we hope, you'll be
tempted to ((experiment)) and go beyond the exercises.
ifdef::interactive_target[]
When reading this book in your browser, you can edit (and run) all
example programs by clicking them.
endif::interactive_target[]
ifdef::book_target[]
(((download)))(((sandbox)))(((running code)))The easiest way to run
the example code in the book, and to experiment with it, is to look it
up in the online version of the book at
http://eloquentjavascript.net/[_eloquentjavascript.net_]. There, you
can click any code example to edit and run it and to see the
output it produces. To work on the exercises, go to
http://eloquentjavascript.net/code[_eloquentjavascript.net/code_],
which provides starting code for each coding exercise and allows you
to look at the solutions.
endif::book_target[]
== Typographic conventions ==
(((factorial function)))In this book, text written in a `monospaced`
font will represent elements of programs—sometimes
they are self-sufficient fragments, and sometimes they just refer to
part of a nearby program. Programs are written as follows:
[source,javascript]
----
function fac(n) {
if (n == 0)
return 1;
else
return fac(n - 1) * n;
}
----
(((print)))Sometimes, in order to show the output that a program
produces, the expected output is written after it, with two slashes
and an arrow in front.
[source,javascript]
----
print(fac(8));
// → 40320
----
Good luck!