Table Of ContentMaster the Language
Eloquent JavaScript
of the Web
E A Modern Introduction
JavaScript is at the heart of almost every ✒ Script the browser and make basic Web
l
modern Web application, whether it’s Google applications o
to Programming
Apps, Twitter, or the newest browser-based q
✒ Work with tools like regular expressions and
game. Though it’s simple for beginners to pick
u
XMLHttpRequest objects
up and play with, JavaScript is not a toy — it’s a
e
flexible and complex language that can be used And since programming is an art that’s best
n
to build full-scale applications. learned by doing, all example code is avail-
Marijn Haverbeke
t
able online in an interactive sandbox for you to
Eloquent JavaScript dives into this
experiment with. With Eloquent JavaScript J
flourishing language and teaches you to
as your guide, you can tweak, expand, and a
write code that’s beautiful and effective.
modify the author’s code, or throw it away and v
By immersing you in example code and
build your own creations from scratch. Before a
encouraging experimentation right from the
you know it, you’ll be fluent in the language of S
start, the author quickly gives you the tools
the Web.
you need to build your own programs. As you c
follow along with examples like an artificial life About the Author r
simulation and a version of the classic game i
p
Sokoban, you’ll learn to: Marijn Haverbeke is a programming language
t
enthusiast and polyglot. He’s worked his way
✒ Understand the essential elements of
from trivial BASIC games on the Commodore,
programming: syntax, control, and data
through a C++ phase, to the present where he
✒ Use object-oriented and functional mostly hacks on database systems and web
programming techniques to organize and APIs in dynamic languages. He created and
clarify your programs maintains several popular open-source projects. H
a
v
e
r
b
e
k
e
THE FINEST IN GEEK ENTERTAINMENT™
www.nostarch.com
$29.95 ($34.95 CDN) Shelve In: Programming Languages/JavaScript
“I LIE FLAT.”
This book uses a lay-flat binding that won't snap shut.
FSC LOGO
ELOQUENT JAVASCRIPT
Pages from EJS_FINAL_rom.pdf 1 12/9/2010 3:27:29 PM
Pages from EJS_FINAL_rom.pdf 2 12/9/2010 3:27:29 PM
Eloquent JavaScript
A Modern Introduction
to Programming
Marijn Haverbeke
Pages from EJS_FINAL_rom.pdf 3 12/9/2010 3:27:29 PM
ELOQUENTJAVASCRIPT.Copyright©2011byMarijnHaverbeke.
Allrightsreserved.Nopartofthisworkmaybereproducedortransmittedinanyformorbyanymeans,electronic
ormechanical,includingphotocopying,recording,orbyanyinformationstorageorretrievalsystem,withoutthe
priorwrittenpermissionofthecopyrightownerandthepublisher.
1514131211 123456789
ISBN-10:1-59327-282-0
ISBN-13:978-1-59327-282-1
Publisher:WilliamPollock
ProductionEditor:SerenaYang
CoverDesign:YourNeighbours
InteriorDesign:OctopodStudios
DevelopmentalEditor:KeithFancher
TechnicalReviewer:PatrickCorcoran
Copyeditor:KimWimpsett
Compositor:SerenaYang
Proofreader:NancySixsmith
Forinformationonbookdistributorsortranslations,pleasecontactNoStarchPress,Inc.directly:
NoStarchPress,Inc.
38RingoldStreet,SanFrancisco,CA94103
phone:415.863.9900;fax:415.863.9950;[email protected];www.nostarch.com
LibraryofCongressCataloging-in-PublicationData
Haverbeke, Marijn.
Eloquent JavaScript: a modern introduction to programming / by Marijn Haverbeke.
p. cm.
Includes index.
ISBN-13: 978-1-59327-282-1
ISBN-10: 1-59327-282-0
1. JavaScript (Computer program language) I. Title.
QA76.73.J39H38 2009
005.13’3-dc22
2010032246
NoStarchPressandtheNoStarchPresslogoareregisteredtrademarksofNoStarchPress,Inc.Otherproductand
companynamesmentionedhereinmaybethetrademarksoftheirrespectiveowners.Ratherthanuseatrademark
symbolwitheveryoccurrenceofatrademarkedname,weareusingthenamesonlyinaneditorialfashionandtothe
benefitofthetrademarkowner,withnointentionofinfringementofthetrademark.
Theinformationinthisbookisdistributedonan“AsIs”basis,withoutwarranty.Whileeveryprecautionhasbeen
takeninthepreparationofthiswork,neithertheauthorsnorNoStarchPress,Inc.shallhaveanyliabilitytoany
personorentitywithrespecttoanylossordamagecausedorallegedtobecauseddirectlyorindirectlybytheinfor-
mationcontainedinit.
Pages from EJS_FINAL_rom.pdf 4 12/9/2010 3:27:29 PM
TolittleLotte.
Pages from EJS_FINAL_rom.pdf 5 12/9/2010 3:27:29 PM
Pages from EJS_FINAL_rom.pdf 6 12/9/2010 3:27:29 PM
BRIEF CONTENTS
Introduction......................................................................... 1
Chapter1:BasicJavaScript: Values,Variables,andControlFlow ........................ 9
Chapter2:Functions................................................................. 29
Chapter3:DataStructures: ObjectsandArrays ........................................ 41
Chapter4:ErrorHandling............................................................ 63
Chapter5:FunctionalProgramming ................................................... 71
Chapter6:Object-OrientedProgramming.............................................. 93
Chapter7:Modularity ...............................................................129
Chapter8:RegularExpressions .......................................................139
Chapter9:WebProgramming: ACrashCourse........................................151
Chapter10:TheDocumentObjectModel..............................................163
Chapter11:BrowserEvents ..........................................................173
Chapter12:HTTPrequests ...........................................................189
Index...............................................................................197
Pages from EJS_FINAL_rom.pdf 7 12/9/2010 3:27:29 PM
Pages from EJS_FINAL_rom.pdf 8 12/9/2010 3:27:29 PM
Description:javascript is the language of the Web, and its at the heart of every modern website from the lowliest personal blog to the mighty Google Apps. Though its simple for beginners to pick up and play with, javascript is not a toy—its a flexible and complex language, capable of much more than the showy