Eloquent JavaScript 4th Edition Marijn Haverbekedownload
Eloquent JavaScript 4th Edition Marijn Haverbekedownload
pdf download
https://ebookmass.com/product/eloquent-javascript-4th-edition-
marijn-haverbeke/
JavaScript T. J. Crowder
https://ebookmass.com/product/javascript-t-j-crowder/
https://ebookmass.com/product/javascript-a-beginners-guide-fifth-
edition-pollock/
https://ebookmass.com/product/how-javascript-works-master-the-basics-
of-javascript-and-modern-web-app-development-1st-edition-jonathon-
simpson-2/
How JavaScript Works: Master the Basics of JavaScript and
Modern Web App Development 1st Edition Jonathon Simpson
https://ebookmass.com/product/how-javascript-works-master-the-basics-
of-javascript-and-modern-web-app-development-1st-edition-jonathon-
simpson/
https://ebookmass.com/product/javascript-a-beginners-guide-fifth-
edition-john-pollock/
https://ebookmass.com/product/javascript-design-patterns-hugo-di-
francesco/
https://ebookmass.com/product/javascript-the-new-toys-t-j-crowder/
https://ebookmass.com/product/javascript-all-in-one-for-dummies-1st-
edition-chris-minnick/
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
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
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
2. Store the number 1 in memory location 1.
3. Store the value of memory location 1 in memory location 2.
4. Subtract the number 11 from the value in memory location 2.
5. If the value in memory location 2 is the number 0, continue with instruc-
tion 9.
6. Add the value of memory location 1 to memory location 0.
7. Add the number 1 to the value of memory location 1.
8. Continue with instruction 3.
9. Output the value of memory location 0.
Although that is already more readable than the soup of bits, it is still rather
obscure. Using names instead of numbers for the instructions and memory
locations helps:
Set “total” to 0.
Set “count” to 1.
[loop]
Set “compare” to “count”.
Subtract 11 from “compare”.
If “compare” is zero, continue at [end].
Add “count” to “total”.
Add 1 to “count”.
Continue at [loop].
[end]
Output “total”.
Can you see how the program works at this point? The first two lines give
two memory locations their starting values: total will be used to build up the
result of the computation, and count will keep track of the number that we are
currently looking at. The lines using compare are probably the most confusing
ones. The program wants to see whether count is equal to 11 to decide whether
it can stop running. Because our hypothetical machine is rather primitive, it
can only test whether a number is zero and make a decision based on that.
It therefore uses the memory location labeled compare to compute the value
of count - 11 and makes a decision based on that value. The next two lines
add the value of count to the result and increment count by 1 every time the
program decides that count is not 11 yet.
Here is the same program in JavaScript:
4
let total = 0, count = 1;
while (count <= 10) {
total += count;
count += 1;
}
console.log(total);
// → 55
The moral of this story is that the same program can be expressed in both long
and short, unreadable and readable ways. The first version of the program was
extremely obscure, whereas this last one is almost English: log the sum of the
range of numbers from 1 to 10. (We will see in later chapters how to define
operations like sum and range.)
A good programming language helps the programmer by allowing them to
talk about the actions that the computer has to perform on a higher level.
It helps omit details, provides convenient building blocks (such as while and
console.log), allows you to define your own building blocks (such as sum and
range), and makes those blocks easy to compose.
What is JavaScript?
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—
5
that is, applications with which you can interact directly without doing a page
reload for every action. JavaScript is also used in more traditional websites to
provide various forms of interactivity and cleverness.
It is important to note that JavaScript has almost nothing to do with the
programming language named Java. The similar name was inspired by mar-
keting 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.
After its adoption outside of Netscape, a standard document was written to
describe the way the JavaScript language should work so that the various pieces
of software that claimed to support JavaScript could make sure they actually
provided the same language. This is called the ECMAScript standard, after
the Ecma International organization that conducted the standardization. In
practice, the terms ECMAScript and JavaScript can be used interchangeably—
they are two names for the same language.
There are those who will say terrible things about JavaScript. Many of these
things are true. When I was required to write something in JavaScript for the
first time, I quickly came to despise it. It would accept almost anything I typed
but interpret it in a way that was completely different from what I meant. This
had a lot to do with the fact that I did not have a clue what I was doing, of
course, but there is a real issue here: JavaScript is ridiculously liberal in what
it allows. The idea behind this design was that it would make programming in
JavaScript easier for beginners. In actuality, it mostly makes finding problems
in your programs harder because the system will not point them out to you.
This flexibility also has its advantages, though. It leaves room for techniques
that are impossible in more rigid languages and makes for a pleasant, informal
style of programming. After learning the language properly and working with
it for a while, I have come to actually like JavaScript.
There have been several versions of JavaScript. ECMAScript version 3 was
the widely supported version during JavaScript’s ascent to dominance, roughly
between 2000 and 2010. During this time, work was underway on an ambitious
version 4, which planned a number of radical improvements and extensions to
the language. Changing a living, widely used language in such a radical way
turned out to be politically difficult, and work on the version 4 was abandoned
in 2008. A much less ambitious version 5, which made only some uncontro-
versial improvements, came out in 2009. In 2015, version 6 came out, a major
update that included some of the ideas planned for version 4. Since then we’ve
had new, small updates every year.
The fact that JavaScript is evolving means that browsers have to constantly
6
keep up. If you’re using an older browser, it may not support every feature.
The language designers are careful to not make any changes that could break
existing programs, so new browsers can still run old programs. In this book,
I’m using the 2023 version of JavaScript.
Web browsers are not the only platforms on which JavaScript is used. Some
databases, such as MongoDB and CouchDB, use JavaScript as their scripting
and query language. Several platforms for desktop and server programming,
most notably the Node.js project (the subject of Chapter 20), provide an envi-
ronment for programming JavaScript outside of the browser.
7
Overview of this book
This book contains roughly three parts. The first 12 chapters discuss the
JavaScript language. The next seven chapters are about web browsers and the
way JavaScript is used to program them. Finally, two chapters are devoted to
Node.js, another environment to program JavaScript in. There are five project
chapters in the book that describe larger example programs to give you a taste
of actual programming.
The language part of the book starts with four chapters that introduce the
basic structure of the JavaScript language. They discuss control structures
(such as the while word you saw in this introduction), functions (writing your
own building blocks), and data structures. After these, you will be able to write
basic programs. Next, Chapters 5 and 6 introduce techniques to use functions
and objects to write more abstract code and keep complexity under control.
After a first project chapter that builds a crude delivery robot, the language
part of the book continues with chapters on error handling and bug fixing,
regular expressions (an important tool for working with text), modularity (an-
other defense against complexity), and asynchronous programming (dealing
with events that take time). The second project chapter, where we implement
a programming language, concludes the first part of the book.
The second part of the book, Chapters 13 to 19, describes the tools that
browser JavaScript has access to. You’ll learn to display things on the screen
(Chapters 14 and 17), respond to user input (Chapter 15), and communicate
over the network (Chapter 18). There are again two project chapters in this
part, building a platform game and a pixel paint program.
Chapter 20 describes Node.js, and Chapter 21 builds a small website using
that tool.
Typographic conventions
In this book, text written in a monospaced font will represent elements of pro-
grams. Sometimes these are self-sufficient fragments, and sometimes they just
refer to part of a nearby program. Programs (of which you have already seen
a few) are written as follows:
function factorial(n) {
if (n == 0) {
return 1;
} else {
return factorial(n - 1) * n;
}
8
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Exploring the Variety of Random
Documents with Different Content
injurious to boilers. 3dly, The only attention required is to fill the coal
receiver, every two or three hours, and clean the fire when
necessary. 4thly, The coal is more completely consumed than by the
common furnace, as all the effect of what is termed stirring up the
fire (by which no inconsiderable quantity of coal is passed into the
ash-pit) is attained without moving the coal upon the grate."
The supply of water which turns the wheel which works the grate
and the machinery in the hopper, is regulated by a cock connected
with the self-regulating damper; so that when the steam is being
produced too fast, the supply of water will be diminished, and by
that means the supply of fuel to the grate will be diminished, and
the grate will revolve less rapidly: and when the steam is being
produced too slowly for the demands of the engine, the contrary
effects take place. In this way the fuel which is introduced into the
furnace is exactly proportioned to the work which the engine has to
perform. The hopper may be made large enough to hold coals for a
day's work, so that the furnace requires no other attendance than to
deposite coals in the hopper each morning.
The coals are let down from the hopper on the grate at that part
which is most remote from the flue; and as they descend in very
small quantities at a time, they are almost immediately ignited. But
until their ignition is complete, a smoke will arise, which, passing to
the flue over the vividly burning fuel, will be ignited. Air is admitted
through proper apertures, and its quantity regulated by the damper
in the same manner as the supply of fuel.
Let C, fig. 41., be the centre of the great working beam, carrying
two arch heads, on which the chains of the piston rods play. The
distances of these arch heads from the centre C must be in the same
proportion as the length of the cylinders, in order that the same play
of the beam may correspond to the plays of both pistons. Let F be
the steam-pipe from the boiler, and G a valve to admit the steam
above the lesser piston, H is a tube by which a communication may
be opened by the valve I between the top and bottom of the lesser
cylinder B. K is a tube communicating, by the valve L, between the
bottom of the lesser cylinder B and the top of the greater cylinder A.
M is a tube communicating, by the valve N, between the top and
bottom of the greater cylinder A; and P a tube leading to the
condenser by the exhausting valve O.
On the other hand, the force which urges the greater piston is
continually decreasing, since there is a vacuum below it, and the
steam which presses it is continually expanding into an increased
bulk.
Let us suppose that the air is blown from the engine in the usual
way, all the valves closed, and the engine ready to start, the pistons
being at the top of the cylinders. Open the valves C, E, and F. The
steam which occupies the greater cylinder below the piston will now
pass into the condenser through F, leaving a vacuum below the
piston. The steam which is in the lesser cylinder below the piston
will pass through D and open the valve E, and will press down the
greater piston. The steam from the boiler will flow in at C, and press
on the lesser piston. At first the whole motion will proceed from the
pressure upon the greater piston, since the steam, both above and
below the lesser piston, has the same pressure. But, as the pistons
descend, the steam below the less passing into the greater cylinder,
expands into a greater space, and consequently exerts a diminished
pressure, and, therefore, the steam on the other side exerting an
undiminished pressure, acquires an impelling force exactly equal to
the pressure lost in the expansion of the steam between the two
pistons. Thus both pistons will be pressed to the bottoms of their
respective cylinders. It will be observed that in the descent the
greater piston is urged by a continually decreasing force, while the
lesser is urged by continually increasing force.
Upon the arrival of the pistons at the bottoms of the cylinders, let
the valves, C, E, F be closed, and C´, E´, F´ be opened, as in fig.
44. The steam which is above the greater piston now flows through
F´ into the condenser, leaving the space above the piston a vacuum.
The steam which is above the lesser piston passes through E´ and D
´ below the greater, while the steam from the boiler is admitted
through C´ below the lesser piston. The pressure of the steam
entering through E´ below the greater piston, pressing on it against
the vacuum above it, commences the ascent. In the mean time the
steam above the lesser piston passing into the enlarged space of the
greater cylinder, loses gradually its elastic force, so that the steam
entering from the boiler at C´ becomes in part effective, and the
ascent is completed under exactly the same circumstances as the
descent, and in this way the process is continued.
Pl. IX.
Pl. X.
The air-pump is placed immediately under the cylinder, and the
continuation of the piston-rod works its piston, which is solid and
without a valve: F is the pipe from the condenser to the air-pump,
through which the condensed steam is drawn off through the valve
G on the ascent of the piston, and on the descent, this is forced
through a tube into a hot well, H, for the purpose of feeding the
boiler through the feed-pipe I. In the top of the hot well H is a valve
which opens inwards, and is kept closed by a ball floating on the
surface of the liquid. The pressure of the condensed air above the
surface of the liquid in H forces it through I into the boiler. When the
air accumulates in too great a degree in H, the surface of the liquid
is pressed so low that the ball falls and opens the valve, and allows
it to escape. The air in H is that which is pumped from the
condenser with the liquid, and which was disengaged from it.
Let us suppose the piston at the top of the cylinder; it strikes the
tail of the valve T, and raises it, while the stem of the piston-valve R
strikes the top of the cylinder, and is pressed into its seat. A free
communication is at the same time open between the cylinder,
below the piston and the condenser, through the tube D. The
pressure of the steam thus admitted above the piston, acting against
the vacuum below it, will cause its descent. On arriving at the
bottom of the cylinder, the tail of the piston-valve R will strike the
bottom, and it will be lifted from its seat, so that a communication
will be opened through it with the condenser. At the same moment a
projecting spring, K, attached to the piston-rod, strikes the stem of
the steam-valve T, and presses it into its seat. Thus, while the
further admission of steam is cut off, the steam above the piston
flows into the condenser, and the piston being relieved from all
pressure, is drawn up by the momentum of the fly-wheel, which
continues the motion it received from the descending force. On the
arrival of the piston again at the top of the cylinder, the valve T is
opened and R closed, and the piston descends as before, and so the
process is continued.
One of the peculiarities of this engine is, that the liquid which is
used for the production of steam in the boiler circulates through the
machine without either diminution or admixture with any other fluid,
so that the boiler never wants more feeding than what can be
supplied from the hot-well H. This circumstance forms a most
important feature in the machine, as it allows of ardent spirits being
used in the boiler instead of water, which, since they boil at low
heats, promised a saving of half the fuel. The inventor even
proposed, that the engine should be used as a still, as well as a
mechanical power, in which case the whole of the fuel would be
saved.
In this engine, the ordinary method of rendering the piston steam-
tight, by oil or melted wax or tallow poured upon it, could not be
applied, since the steam above the piston must always have a free
passage through the piston-valve R. The ingenious inventor
therefore contrived a method of making the piston steam-tight in the
cylinder, without oil or stuffing, and his method has since been
adopted with success in other engines.
Metallic pistons have lately come into very general use, and such
contrivances differ very little from the above.
CHAPTER X.
LOCOMOTIVE ENGINES ON RAILWAYS.
Pl. XI.
Engr. by P. Maverick
(e) To the plates of the English edition has been added one, (plate
A) representing a high-pressure engine as constructed by the West
Point Foundry in the state of New York. The principal parts will be
readily distinguished from their resemblance to the analogous parts
of a condensing engine. The condenser and air-pump of that engine,
it will be observed, are suppressed. At v x and y z are forcing-pumps
by which a supply of water is injected into the boiler at each motion
of the engine. For the four-way cock, used in the English high-
pressure engines, a slide valve at r s, is substituted, and is found to
work to much greater advantage. It is set in motion by an eccentric,
in a manner that will be more obvious from an inspection of the
plate than from any description.—A. E.
(83.) Two years after the date of the patent of this engine, its
inventor constructed a machine of the same kind for the purpose of
moving carriages on railroads; and applied it successfully, in the year
1804, on the railroad at Merthyr Tydvil, in South Wales. It was in
principle the same as that already described. The cylinder however
was in a horizontal position, the piston-rod working in the direction
of the line of road: the extremity of the piston-rod, by means of a
connecting rod, worked cranks placed on the axletree, on which
were fixed two cogged wheels: these worked in others, by which
their motion was communicated finally to cogged wheels fixed on
the axle of the hind wheels of the carriage, by which this axle was
kept in a state of revolution. The hind wheels being fixed on the
axletree, and turning with it, were caused likewise to revolve; and so
long as the weight of the carriage did not exceed that which the
friction of the road was capable of propelling, the carriage would
thus be moved forwards. On this axle was placed a fly-wheel to
continue the rotatory motion at the termination of each stroke. The
fore wheels are described as being capable of turning like the fore
wheels of a carriage, so as to guide the vehicle. The projectors
appear to have contemplated, in the first instance, the use of this
carriage on turnpike roads; but that notion seems to have been
abandoned, and its use was only adopted on the railroad before
mentioned. On the occasion of its first trial, it drew after it as many
carriages as contained 10 tons of iron a distance of nine miles;
which stage it performed without any fresh supply of water, and
travelled at the rate of five miles an hour.
(84.) Capital and skill have of late years been directed with
extraordinary energy to the improvement of inland transport; and
this important instrument of national wealth and civilisation has
received a proportionate impulse. Effects are now witnessed, which,
had they been narrated a few years since, could only have been
admitted into the pages of fiction or volumes of romance. Who could
have credited the possibility of a ponderous engine of iron, loaded
with several hundred passengers, in a train of carriages of
corresponding magnitude, and a large quantity of water and coal,
taking flight from Manchester and arriving at Liverpool, a distance of
about thirty miles, in little more than an hour? And yet this is a
matter of daily and almost hourly occurrence. Neither is the road, on
which this wondrous performance is effected, the most favourable
which could be constructed for such machines. It is subject to
undulations and acclivities, which reduce the rate of speed much
more than similar inequalities affect the velocity on common roads.
The rapidity of transport thus attained is not less wonderful than the
weights transported. Its capabilities in this respect far transcend the
exigencies even of the two greatest commercial marts in Great
Britain. Loads, varying from 50 to 150 tons are transported at the
average rate of 15 miles an hour; but the engines in this case are
loaded below their power; and in one instance we have seen a load
—we should rather say a cargo—of wagons, conveying merchandise
to the amount of 230 tons gross, transported from Liverpool to
Manchester at the average rate of 12 miles an hour.
The astonishment with which such performances must be viewed,
might be qualified, if the art of transport by steam on railways had
been matured, and had attained that full state of perfection which
such an art is always capable of receiving from long experience,
aided by great scientific knowledge, and the unbounded application
of capital. But such is not the present case. The art of constructing
locomotive engines, so far from having attained a state of maturity,
has not even emerged from its infancy. So complete was the
ignorance of its powers which prevailed, even among engineers,
previous to the opening of the Liverpool railway, that the transport of
heavy goods was regarded as the chief object of the undertaking,
and its principal source of revenue. The incredible speed of
transport, effected even in the very first experiments in 1830, burst
upon the public, and on the scientific world, with all the effect of a
new and unlooked-for phenomenon. On the unfortunate occasion
which deprived this country of Mr. Huskisson, the wounded body of
that statesman was transported a distance of about fifteen miles in
twenty-five minutes, being at the rate of thirty-six miles an hour. The
revenue of the road arising from passengers since its opening, has,
contrary to all that was foreseen, been nearly double that which has
been derived from merchandise. So great was the want of
experience in the construction of engines, that the company was at
first ignorant whether they should adopt large steam-engines fixed
at different stations on the line, to pull the carriages from station to
station, or travelling engines to drag the loads the entire distance.
Having decided on the latter, they have, even to the present
moment, laboured under the disadvantage of the want of that
knowledge which experience alone can give. The engines have been
constantly varied in their weight and proportions, in their magnitude
and form, as the experience of each successive month has indicated.
As defects became manifest they were remedied; improvements
suggested were adopted; and each quarter produced engines of
such increased power and efficiency, that their predecessors were
abandoned, not because they were worn out, but because they had
been outstripped in the rapid march of improvement. Add to this,
that only one species of travelling engine has been effectively tried;
the capabilities of others remain still to be developed; and even that
form of engine which has received the advantage of a course of
experiments on so grand a scale to carry it towards perfection, is far
short of this point, and still has defects, many of which, it is obvious,
time and experience will remove. If then travelling steam engines,
with all the imperfections of an incipient invention—with the want of
experience, the great parent of practical improvements—with the
want of the common advantage of the full application of the skill and
capital of the country—subjected to but one great experiment, and
that experiment limited to one form of engine; if, under such
disadvantages, the effects to which we have referred have been
produced, what may we not expect from this extraordinary power,
when the enterprise of the country shall be unfettered, when greater
fields of experience are opened, when time, ingenuity, and capital
have removed the existing imperfections, and have brought to light
new and more powerful principles? This is not mere speculation on
possibilities, but refers to what is in a state of actual progression.
Railways are in progress between the points of greatest intercourse
in the United Kingdom, and travelling steam engines are in
preparation for the common turnpike roads; the practicability and
utility of that application of the steam engine having not only been
established by experiment to the satisfaction of their projectors, but
proved before the legislature in a committee of inquiry on the
subject.
It has been said, that in Great Britain there are above a million of
horses engaged in various ways in the transport of passengers and
goods, and that to support each horse requires as much land as
would, upon an average, support eight men. If this quantity of
animal power were displaced by steam engines, and the means of
transport drawn from the bowels of the earth, instead of being
raised upon its surface, then, supposing the above calculation
correct, as much land would become available for the support of
human beings as would suffice for an additional population of eight
millions; or, what amounts to the same, would increase the means of
support of the present population by about one-third of the present
available means. The land which now supports horses for transport
would then support men, or produce corn for food.
ebookmasss.com