Synopses & Reviews
JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.
Eloquent JavaScript, 2nd Edition dives deep into the JavaScript language to show you how to write beautiful, effective code. Author Marijn Haverbeke immerses you in example code from the start, while exercises and full-chapter projects give you hands-on experience with writing your own programs. As you build projects such as an artificial life simulation, a simple programming language, and a paint program, you'll learn:
- The essential elements of programming, including syntax, control, and data
- How to organize and clarify your code with object-oriented and functional programming techniques
- How to script the browser and make basic web applications
- How to use the DOM effectively to interact with browsers
- How to harness Node.js to build servers and utilities
This edition is thoroughly revised and modernized to reflect the current state of JavaScript and web browsers, with brand-new material, such as a chapter on code performance in JavaÂScript, and expanded coverage of recursion and closures. All source code is available online in an interactive sandbox, where you can edit the code, run it, and see its output instantly.
Isn't it time you became fluent in the language of the Web?
Synopsis
JavaScript is at the heart of almost every modern Web application, whether it's Google Apps, Twitter, or the newest browser-based game. Though it's simple for beginners to pick up and play with, JavaScript is not a toy—it's a flexible, complex language that you can use to build full-scale applications.
Eloquent JavaScript, 2nd Edition dives into the language and teaches you to write beautiful, effective code. By immersing you in example code and encouraging experimentation right from the start, the author quickly provides the tools you need to build your own programs. As you follow along with examples like an artificial life simulation, a simple programming language, and a platform game, you'll learn to:
- Understand the essential elements of programming: syntax, control, and data
- Organize and clarify your code with object-oriented and functional programming techniques
- Script the browser and make basic Web applications
- Harness node.js to build servers and utilities
- Use the DOM effectively to interact with browsers
This new edition is thoroughly revised and modernized, and it's full of brand-new material, including expanded content on recursion, closures, and more. All source code is available online in an interactive sandbox and has been updated to follow current JavaScript idioms. With
Eloquent JavaScript, 2nd Edition as your guide, you'll be fluent in the language of the Web before you know it.
About the Author
Marijn Haverbeke is an independent developer and author, focused primarily on programming languages and tools for programmers. He spends most of his time working on open source software, such as the CodeMirror editor and the Tern type inference engine.
Table of Contents
; Praise for the First Edition of Eloquent Javascript; Introduction; On Programming; Why Language Matters; What Is JavaScript?; Code, and What to Do with It; Overview of This Book; Typographic Conventions; Language; Chapter 1: Values, Types, and Operators; 1.1 Values; 1.2 Numbers; 1.3 Strings; 1.4 Unary Operators; 1.5 Boolean Values; 1.6 Undefined Values; 1.7 Automatic Type Conversion; 1.8 Summary; Chapter 2: Program Structure; 2.1 Expressions and Statements; 2.2 Variables; 2.3 Keywords and Reserved Words; 2.4 The Environment; 2.5 Functions; 2.6 The console.log Function; 2.7 Return Values; 2.8 Prompt and Confirm; 2.9 Control Flow; 2.10 Conditional Execution; 2.11 while and do Loops; 2.12 Indenting Code; 2.13 for Loops; 2.14 Breaking Out of a Loop; 2.15 Updating Variables Succinctly; 2.16 Dispatching on a Value with switch; 2.17 Capitalization; 2.18 Comments; 2.19 Summary; 2.20 Exercises; Chapter 3: Functions; 3.1 Defining a Function; 3.2 Parameters and Scopes; 3.3 Nested Scopes; 3.4 Functions as Values; 3.5 Declaration Notation; 3.6 The Call Stack; 3.7 Optional Arguments; 3.8 Closure; 3.9 Recursion; 3.10 Growing Functions; 3.11 Functions and Side Effects; 3.12 Summary; 3.13 Exercises; Chapter 4: Data Structures: Objects and Arrays; 4.1 The Weresquirrel; 4.2 Data Sets; 4.3 Properties; 4.4 Methods; 4.5 Objects; 4.6 Mutability; 4.7 The Lycanthrope's Log; 4.8 Computing Correlation; 4.9 Objects as Maps; 4.10 The Final Analysis; 4.11 Further Arrayology; 4.12 Strings and Their Properties; 4.13 The arguments Object; 4.14 The Math Object; 4.15 The Global Object; 4.16 Summary; 4.17 Exercises; Chapter 5: Higher-Order Functions; 5.1 Abstraction; 5.2 Abstracting Array Traversal; 5.3 Higher-Order Functions; 5.4 Passing Along Arguments; 5.5 JSON; 5.6 Filtering an Array; 5.7 Transforming with map; 5.8 Summarizing with reduce; 5.9 Composability; 5.10 The Cost; 5.11 Great-great-great-great-...; 5.12 Binding; 5.13 Summary; 5.14 Exercises; Chapter 6: The Secret Life of Objects; 6.1 History; 6.2 Methods; 6.3 Prototypes; 6.4 Constructors; 6.5 Overriding Derived Properties; 6.6 Prototype Interference; 6.7 Prototype-less Objects; 6.8 Polymorphism; 6.9 Laying Out a Table; 6.10 Getters and Setters; 6.11 Inheritance; 6.12 The instanceof Operator; 6.13 Summary; 6.14 Exercises; Chapter 7: Project: Electronic Life; 7.1 Definition; 7.2 Representing Space; 7.3 A Critter's Programming Interface; 7.4 The World Object; 7.5 this and Its Scope; 7.6 Animating Life; 7.7 It Moves; 7.8 More Life-forms; 7.9 A More Lifelike Simulation; 7.10 Action Handlers; 7.11 Populating the New World; 7.12 Bringing the World to Life; 7.13 Exercises; Chapter 8: Bugs and Error Handling; 8.1 Programmer Mistakes; 8.2 Strict Mode; 8.3 Testing; 8.4 Debugging; 8.5 Error Propagation; 8.6 Exceptions; 8.7 Cleaning Up After Exceptions; 8.8 Selective Catching; 8.9 Assertions; 8.10 Summary; 8.11 Exercises; Chapter 9: Regular Expressions; 9.1 Creating a Regular Expression; 9.2 Testing for Matches; 9.3 Matching a Set of Characters; 9.4 Repeating Parts of a Pattern; 9.5 Grouping Subexpressions; 9.6 Matches and Groups; 9.7 The Date Type; 9.8 Word and String Boundaries; 9.9 Choice Patterns; 9.10 The Mechanics of Matching; 9.11 Backtracking; 9.12 The replace Method; 9.13 Greed; 9.14 Dynamically Creating RegExp Objects; 9.15 The search Method; 9.16 The lastIndex Property; 9.17 Parsing an INI File; 9.18 International Characters; 9.19 Summary; 9.20 Exercises; Chapter 10: Modules; 10.1 Why Modules Help; 10.2 Using Functions as Namespaces; 10.3 Objects as Interfaces; 10.4 Detaching from the Global Scope; 10.5 Evaluating Data as Code; 10.6 The require Function; 10.7 Slow-Loading Modules; 10.8 Interface Design; 10.9 Summary; 10.10 Exercises; Chapter 11: Project: A Programming Language; 11.1 Parsing; 11.2 The Evaluator; 11.3 Special Forms; 11.4 The Environment; 11.5 Functions; 11.6 Compilation; 11.7 Cheating; 11.8 Exercises; Browser; Chapter 12: Javascript and The Browser; 12.1 Networks and the Internet; 12.2 The Web; 12.3 HTML; 12.4 HTML and JavaScript; 12.5 In the Sandbox; 12.6 Compatibility and the Browser Wars; Chapter 13: The Document Object Model; 13.1 Document Structure; 13.2 Trees; 13.3 The Standard; 13.4 Moving Through the Tree; 13.5 Finding Elements; 13.6 Changing the Document; 13.7 Creating Nodes; 13.8 Attributes; 13.9 Layout; 13.10 Styling; 13.11 Cascading Styles; 13.12 Query Selectors; 13.13 Positioning and Animating; 13.14 Summary; 13.15 Exercises; Chapter 14: Handling Events; 14.1 Event Handlers; 14.2 Events and DOM Nodes; 14.3 Event Objects; 14.4 Propagation; 14.5 Default Actions; 14.6 Key Events; 14.7 Mouse Clicks; 14.8 Mouse Motion; 14.9 Scroll Events; 14.10 Focus Events; 14.11 Load Event; 14.12 Script Execution Timeline; 14.13 Setting Timers; 14.14 Debouncing; 14.15 Summary; 14.16 Exercises; Chapter 15: Project: A Platform Game; 15.1 The Game; 15.2 The Technology; 15.3 Levels; 15.4 Reading a Level; 15.5 Actors; 15.6 Encapsulation as a Burden; 15.7 Drawing; 15.8 Motion and Collision; 15.9 Actors and Actions; 15.10 Tracking Keys; 15.11 Running the Game; 15.12 Exercises; Chapter 16: Drawing on Canvas; 16.1 SVG; 16.2 The Canvas Element; 16.3 Filling and Stroking; 16.4 Paths; 16.5 Curves; 16.6 Drawing a Pie Chart; 16.7 Text; 16.8 Images; 16.9 Transformation; 16.10 Storing and Clearing Transformations; 16.11 Back to the Game; 16.12 Choosing a Graphics Interface; 16.13 Summary; 16.14 Exercises; Chapter 17: HTTP; 17.1 The Protocol; 17.2 Browsers and HTTP; 17.3 XMLHttpRequest; 17.4 Sending a Request; 17.5 Asynchronous Requests; 17.6 Fetching XML Data; 17.7 HTTP Sandboxing; 17.8 Abstracting Requests; 17.9 Promises; 17.10 Appreciating HTTP; 17.11 Security and HTTPS; 17.12 Summary; 17.13 Exercises; Chapter 18: Forms and Form Fields; 18.1 Fields; 18.2 Focus; 18.3 Disabled Fields; 18.4 The Form as a Whole; 18.5 Text Fields; 18.6 Checkboxes and Radio Buttons; 18.7 Select Fields; 18.8 File Fields; 18.9 Storing Data Client-Side; 18.10 Summary; 18.11 Exercises; Chapter 19: Project: A Paint Program; 19.1 Implementation; 19.2 Building the DOM; 19.3 The Foundation; 19.4 Tool Selection; 19.5 Color and Brush Size; 19.6 Saving; 19.7 Loading Image Files; 19.8 Finishing Up; 19.9 Exercises; Beyond; Chapter 20: Node.js; 20.1 Background; 20.2 Asynchronicity; 20.3 The node Command; 20.4 Modules; 20.5 Installing with NPM; 20.6 The Filesystem Module; 20.7 The HTTP Module; 20.8 Streams; 20.9 A Simple File Server; 20.10 Error Handling; 20.11 Summary; 20.12 Exercises; Chapter 21: Project: Skill-Sharing Website; 21.1 Design; 21.2 Long Polling; 21.3 HTTP Interface; 21.4 The Server; 21.5 The Client; 21.6 Exercises; Chapter 22: Javascript and Performance; 22.1 Staged Compilation; 22.2 Graph Layout; 22.3 Defining a Graph; 22.4 A First Force-Directed Layout Function; 22.5 Profiling; 22.6 Function Inlining; 22.7 Going Back to Old-school Loops; 22.8 Avoiding Work; 22.9 Creating Less Garbage; 22.10 Garbage Collection; 22.11 Writing to Objects; 22.12 Dynamic Types; 22.13 Summary; 22.14 Exercises; Exercise Hints; Program Structure; Functions; Data Structures: Objects and Arrays; Higher-Order Functions; The Secret Life of Objects; Project: Electronic Life; Bugs and Error Handling; Regular Expressions; Modules; Project: A Programming Language; The Document Object Model; Handling Events; Project: A Platform Game; Drawing on Canvas; HTTP; Forms and Form Fields; Project: A Paint Program; Node.js; Project: Skill-Sharing Website; JavaScript and Performance; Updates;