This repo includes all of the numbered examples from the 7th edition of my book, plus many unnumbered examples as well. In general, if an unnumbered example defines a function that seems like it might be useful to someone, I've included it here. The code is listed below in roughly the same order as in the book. To locate code, just look below for the example number or section number that the code appears in.
This code is far more useful when studied in the context of the book in which it appears, and I'd ask you to please buy yourself a copy if you are able to. These examples are free for everyone to use.
If you discover problems with this repo, or bugs in the code, please open a GitHub issue. (Note, however, that I will not accept pull requests because I need to maintain clear copyright to the code so that I can use it in future editions of the book.)
- Example 1-1: Computing character frequency histograms with JavaScript
- §3.10.3: Destructuring Assignment
- §6.7: Extending Objects
- §6.10.6: Property Getters and Setters
- §8.1: Defining Functions
- §8.3.4: The Spread Operator for Function Calls
- §8.3.5: Destructuring Function Arguments into Parameters
- Example 8-1: Using functions as data
- §8.4.1 Defining Your Own Function Properties
- §8.6 Closures
- Example 8-2: Private property accessor methods using closures
- §8.7.4: The call() and apply() Methods
- §8.8.1: Processing Arrays with Functions
- §8.8.2: Higher-Order Functions
- §8.8.3: Partial Application of Functions
- §8.8.4: Memoization
- Example 9-1: A simple JavaScript class
- Example 9-2: A Range class using a constructor
- Example 9-3: The Range class rewritten using class
- Example 9-4: Complex.js: a complex number class
- Example 9-5: Span.js: a simple subclass of Range
- §9.5.2: Subclasses with extends and super
- Example 9-6: TypedMap.js: a subclass of Map that checks key and value types
- Example 9-7: Histogram.js: a Set-like class implemented with delegation
- Example 9-8: Sets.js: a hierarchy of abstract and concrete set classes
- Example 12-1: An iterable numeric Range class
- §12.2: Implementing Iterable Objects
- §12.3.1: Generator Examples
- §12.3.2 yield* and Recursive Generators
- §13.1.4: Callbacks and Events in Node
- §13.2.6: Making Promises
- Example 13-1: An asynchronous getJSON() function
- §13.2.7 Promises in Sequence
- §13.4.3: Asynchronous Generators
- §13.4.4: Implementing Asynchronous Iterators
- Example 14-1: Copying properties and their attributes from one object to another
- §14.4.3: Symbol.toStringTag
- §14.4.6: Pattern-Matching Symbols
- §14.5: Template Tags
- §14.7: Proxy Objects
- identity
- readOnlyProxy
- loggingProxy
- §15.1.1: JavaScript in HTML <script> Tags
- §15.2.2: Registering Event Handlers
- §15.3.2: Document Structure and Traversal
- Example 15-1: Generating a table of contents with the DOM API
- §15.4.4: Scripting Stylesheets
- Example 15-2: The custom element
- Example 15-3: Implementing a web component
- §15.7.1: SVG in HTML
- §15.7.2: Scripting SVG
- Example 15-4: Drawing a pie chart with JavaScript and SVG
- Example 15-5: Regular polygons with moveTo(), lineTo(), and closePath()
- Example 15-6: Adding curves to a path
- §15.8.5: Coordinate System Transforms
- Example 15-7: A Koch snowflake with transformations
- §15.8.6: Clipping
- Example 15-8: Motion blur with ImageData
- §15.9.2: The WebAudio API
- Example 15-9: History management with pushState()
- §15.11.1: fetch()
- Example 15-10: Streaming the response body from a fetch() request
- Example 15-11: A simple chat client using EventSource
- Example 15-12: A Server Sent Events chat server
- §15.12.2: Cookies
- Example 15-13: A IndexedDB database of US postal codes
- Example 15-14: Worker code for computing regions of the Mandelbrot set
- Example 15-15: A web application for displaying and exploring the Mandelbrot set
- §16.2: Node Is Asynchronous by Default
- §16.5.1: Pipes
- §16.5.2: Asynchronous Iteration
- §16.5.3: Writing to Streams and Handling Backpressure
- §16.5.4: Reading Streams with Events
- §16.7.6: Working with Directories
- §16.8 HTTP: Clients and Servers
- §16.9: Non-HTTP Network Servers and Clients
- §16.10.2: exec() and execFile()
- §16.10.4: fork()
- §16.11.1: Creating Workers and Passing Messages
- §16.11.5: Sharing Typed Arrays Between Threads