You are previewing Eloquent JavaScript, 2nd Edition.
O'Reilly logo
Eloquent JavaScript, 2nd Edition

Book Description

Thoroughly updated and revised, Eloquent JavaScript, 2nd Edition shows you how to write elegant, effective JavaScript code.

Table of Contents

  1. Eloquent JavaScript: A Modern Introduction to Programming
  2. Dedication
  3. Praise for the First Edition of Eloquent Javascript
  4. Introduction
    1. On Programming
    2. Why Language Matters
    3. What Is JavaScript?
    4. Code, and What to Do with It
    5. Overview of This Book
    6. Typographic Conventions
  5. I. Language
    1. 1. Values, Types, and Operators
      1. Values
      2. Numbers
        1. Arithmetic
        2. Special Numbers
      3. Strings
      4. Unary Operators
      5. Boolean Values
        1. Comparisons
        2. Logical Operators
      6. Undefined Values
      7. Automatic Type Conversion
        1. Short-Circuiting of Logical Operators
      8. Summary
    2. 2. Program Structure
      1. Expressions and Statements
      2. Variables
      3. Keywords and Reserved Words
      4. The Environment
      5. Functions
      6. The console.log Function
      7. Return Values
      8. Prompt and Confirm
      9. Control Flow
      10. Conditional Execution
      11. while and do Loops
      12. Indenting Code
      13. for Loops
      14. Breaking Out of a Loop
      15. Updating Variables Succinctly
      16. Dispatching on a Value with switch
      17. Capitalization
      18. Comments
      19. Summary
      20. Exercises
        1. Looping a Triangle
        2. FizzBuzz
        3. Chess Board
    3. 3. Functions
      1. Defining a Function
      2. Parameters and Scopes
      3. Nested Scopes
      4. Functions as Values
      5. Declaration Notation
      6. The Call Stack
      7. Optional Arguments
      8. Closure
      9. Recursion
      10. Growing Functions
      11. Functions and Side Effects
      12. Summary
      13. Exercises
        1. Minimum
        2. Recursion
        3. Bean Counting
    4. 4. Data Structures: Objects and Arrays
      1. The Weresquirrel
      2. Data Sets
      3. Properties
      4. Methods
      5. Objects
      6. Mutability
      7. The Lycanthrope’s Log
      8. Computing Correlation
      9. Objects as Maps
      10. The Final Analysis
      11. Further Arrayology
      12. Strings and Their Properties
      13. The arguments Object
      14. The Math Object
      15. The Global Object
      16. Summary
      17. Exercises
        1. The Sum of a Range
        2. Reversing an Array
        3. A List
        4. Deep Comparison
    5. 5. Higher-Order Functions
      1. Abstraction
      2. Abstracting Array Traversal
      3. Higher-Order Functions
      4. Passing Along Arguments
      5. JSON
      6. Filtering an Array
      7. Transforming with map
      8. Summarizing with reduce
      9. Composability
      10. The Cost
      11. Great-great-great-great-...
      12. Binding
      13. Summary
      14. Exercises
        1. Flattening
        2. Mother-Child Age Difference
        3. Historical Life Expectancy
        4. Every and Then Some
    6. 6. The Secret Life of Objects
      1. History
      2. Methods
      3. Prototypes
      4. Constructors
      5. Overriding Derived Properties
      6. Prototype Interference
      7. Prototype-less Objects
      8. Polymorphism
      9. Laying Out a Table
      10. Getters and Setters
      11. Inheritance
      12. The instanceof Operator
      13. Summary
      14. Exercises
        1. A Vector Type
        2. Another Cell
        3. Sequence Interface
    7. 7. Project: Electronic Life
      1. Definition
      2. Representing Space
      3. A Critter’s Programming Interface
      4. The World Object
      5. this and Its Scope
      6. Animating Life
      7. It Moves
      8. More Life-forms
      9. A More Lifelike Simulation
      10. Action Handlers
      11. Populating the New World
      12. Bringing the World to Life
      13. Exercises
        1. Artificial Stupidity
        2. Predators
    8. 8. Bugs and Error Handling
      1. Programmer Mistakes
      2. Strict Mode
      3. Testing
      4. Debugging
      5. Error Propagation
      6. Exceptions
      7. Cleaning Up After Exceptions
      8. Selective Catching
      9. Assertions
      10. Summary
      11. Exercises
        1. Retry
        2. The Locked Box
    9. 9. Regular Expressions
      1. Creating a Regular Expression
      2. Testing for Matches
      3. Matching a Set of Characters
      4. Repeating Parts of a Pattern
      5. Grouping Subexpressions
      6. Matches and Groups
      7. The Date Type
      8. Word and String Boundaries
      9. Choice Patterns
      10. The Mechanics of Matching
      11. Backtracking
      12. The replace Method
      13. Greed
      14. Dynamically Creating RegExp Objects
      15. The search Method
      16. The lastIndex Property
        1. Looping over Matches
      17. Parsing an INI File
      18. International Characters
      19. Summary
      20. Exercises
        1. Regexp Golf
        2. Quoting Style
        3. Numbers Again
    10. 10. Modules
      1. Why Modules Help
        1. Namespacing
        2. Reuse
        3. Decoupling
      2. Using Functions as Namespaces
      3. Objects as Interfaces
      4. Detaching from the Global Scope
      5. Evaluating Data as Code
      6. The require Function
      7. Slow-Loading Modules
      8. Interface Design
        1. Predictability
        2. Composability
        3. Layered Interfaces
      9. Summary
      10. Exercises
        1. Month Names
        2. A Return to Electronic Life
        3. Circular Dependencies
    11. 11. Project: A Programming Language
      1. Parsing
      2. The Evaluator
      3. Special Forms
      4. The Environment
      5. Functions
      6. Compilation
      7. Cheating
      8. Exercises
        1. Arrays
        2. Closure
        3. Comments
        4. Fixing Scope
  6. II. Browser
    1. 12. Javascript and The Browser
      1. Networks and the Internet
      2. The Web
      3. HTML
      4. HTML and JavaScript
      5. In the Sandbox
      6. Compatibility and the Browser Wars
    2. 13. The Document Object Model
      1. Document Structure
      2. Trees
      3. The Standard
      4. Moving Through the Tree
      5. Finding Elements
      6. Changing the Document
      7. Creating Nodes
      8. Attributes
      9. Layout
      10. Styling
      11. Cascading Styles
      12. Query Selectors
      13. Positioning and Animating
      14. Summary
      15. Exercises
        1. Build a Table
        2. Elements by Tag Name
        3. The Cat’s Hat
    3. 14. Handling Events
      1. Event Handlers
      2. Events and DOM Nodes
      3. Event Objects
      4. Propagation
      5. Default Actions
      6. Key Events
      7. Mouse Clicks
      8. Mouse Motion
      9. Scroll Events
      10. Focus Events
      11. Load Event
      12. Script Execution Timeline
      13. Setting Timers
      14. Debouncing
      15. Summary
      16. Exercises
        1. Censored Keyboard
        2. Mouse Trail
        3. Tabs
    4. 15. Project: A Platform Game
      1. The Game
      2. The Technology
      3. Levels
      4. Reading a Level
      5. Actors
      6. Encapsulation as a Burden
      7. Drawing
      8. Motion and Collision
      9. Actors and Actions
      10. Tracking Keys
      11. Running the Game
      12. Exercises
        1. Game Over
        2. Pausing the Game
    5. 16. Drawing on Canvas
      1. SVG
      2. The Canvas Element
      3. Filling and Stroking
      4. Paths
      5. Curves
      6. Drawing a Pie Chart
      7. Text
      8. Images
      9. Transformation
      10. Storing and Clearing Transformations
      11. Back to the Game
      12. Choosing a Graphics Interface
      13. Summary
      14. Exercises
        1. Shapes
        2. The Pie Chart
        3. A Bouncing Ball
        4. Precomputed Mirroring
    6. 17. HTTP
      1. The Protocol
      2. Browsers and HTTP
      3. XMLHttpRequest
      4. Sending a Request
      5. Asynchronous Requests
      6. Fetching XML Data
      7. HTTP Sandboxing
      8. Abstracting Requests
      9. Promises
      10. Appreciating HTTP
      11. Security and HTTPS
      12. Summary
      13. Exercises
        1. Content Negotiation
        2. Waiting for Multiple Promises
    7. 18. Forms and Form Fields
      1. Fields
      2. Focus
      3. Disabled Fields
      4. The Form as a Whole
      5. Text Fields
      6. Checkboxes and Radio Buttons
      7. Select Fields
      8. File Fields
      9. Storing Data Client-Side
      10. Summary
      11. Exercises
        1. A JavaScript Workbench
        2. Autocompletion
        3. Conway’s Game of Life
    8. 19. Project: A Paint Program
      1. Implementation
      2. Building the DOM
      3. The Foundation
      4. Tool Selection
      5. Color and Brush Size
      6. Saving
      7. Loading Image Files
      8. Finishing Up
      9. Exercises
        1. Rectangles
        2. Color Picker
        3. Flood Fill
  7. III. Beyond
    1. 20. Node.js
      1. Background
      2. Asynchronicity
      3. The node Command
      4. Modules
      5. Installing with NPM
      6. The Filesystem Module
      7. The HTTP Module
      8. Streams
      9. A Simple File Server
      10. Error Handling
      11. Summary
      12. Exercises
        1. Content Negotiation, Again
        2. Fixing a Leak
        3. Creating Directories
        4. A Public Space on the Web
    2. 21. Project: Skill-Sharing Website
      1. Design
      2. Long Polling
      3. HTTP Interface
      4. The Server
        1. Routing
        2. Serving Files
        3. Talks as Resources
        4. Long-Polling Support
      5. The Client
        1. HTML
        2. Starting up
        3. Displaying Talks
        4. Updating the Server
        5. Noticing Changes
      6. Exercises
        1. Disk Persistence
        2. Comment Field Resets
        3. Better Templates
        4. The Unscriptables
    3. 22. Javascript and Performance
      1. Staged Compilation
      2. Graph Layout
      3. Defining a Graph
      4. A First Force-Directed Layout Function
      5. Profiling
      6. Function Inlining
      7. Going Back to Old-school Loops
      8. Avoiding Work
      9. Creating Less Garbage
      10. Garbage Collection
      11. Writing to Objects
      12. Dynamic Types
      13. Summary
      14. Exercises
        1. Pathfinding
        2. Timing
        3. Optimizing
  8. A. Exercise Hints
    1. Program Structure
      1. Looping a Triangle
      2. FizzBuzz
      3. Chess Board
    2. Functions
      1. Minimum
      2. Recursion
      3. Bean Counting
    3. Data Structures: Objects and Arrays
      1. The Sum of a Range
      2. Reversing an Array
      3. A List
      4. Deep Comparison
    4. Higher-Order Functions
      1. Mother-Child Age Difference
      2. Historical Life Expectancy
      3. Every and Then Some
    5. The Secret Life of Objects
      1. A Vector Type
      2. Another Cell
      3. Sequence Interface
    6. Project: Electronic Life
      1. Artificial Stupidity
      2. Predators
    7. Bugs and Error Handling
      1. Retry
      2. The Locked Box
    8. Regular Expressions
      1. Quoting Style
      2. Numbers Again
    9. Modules
      1. Month Names
      2. A Return to Electronic Life
      3. Circular Dependencies
    10. Project: A Programming Language
      1. Arrays
      2. Closure
      3. Comments
      4. Fixing Scope
    11. The Document Object Model
      1. Build a Table
      2. Elements by Tag Name
    12. Handling Events
      1. Censored Keyboard
      2. Mouse Trail
      3. Tabs
    13. Project: A Platform Game
      1. Game Over
      2. Pausing the Game
    14. Drawing on Canvas
      1. Shapes
      2. The Pie Chart
      3. A Bouncing Ball
      4. Precomputed Mirroring
    15. HTTP
      1. Content Negotiation
      2. Waiting for Multiple Promises
    16. Forms and Form Fields
      1. A JavaScript Workbench
      2. Autocompletion
      3. Conway’s Game of Life
    17. Project: A Paint Program
      1. Rectangles
      2. Color Picker
      3. Flood Fill
    18. Node.js
      1. Content Negotiation, Again
      2. Fixing a Leak
      3. Creating Directories
      4. A Public Space on the Web
    19. Project: Skill-Sharing Website
      1. Disk Persistence
      2. Comment Field Resets
      3. Better Templates
      4. The Unscriptables
    20. JavaScript and Performance
      1. Pathfinding
      2. Optimizing
  9. Updates
  10. Index
  11. About the Author
  12. Copyright