JavaScript & TypeScript
JavaScript Cookbook: Programming the Web
Matthew MacDadonald, Adam D. Scott, Shelley Powers, 2021
Inhaltsverzeichnis des Buches
- Preface
- Book Audience
- Book Organization
- Conventions Used in This Book
- Using Code Examples
- O'Reilly Online Learning
- How to Contact Us
- Acknowledgments
- I. The JavaScript Language
- 1. Setting Up a Development Environment
- Choosing a Code Editor
- Using the Developer Console in Your Browser
- Running Blocks of Code in the Developer Console
- Using Strict Mode to Catch Common Mistakes
- Filling in HTML Boilerplate with Emmet Shortcuts
- Installing the npm Package Manager (with Node.js)
- Extra: Using a Terminal and Shell
- Downloading a Package with npm
- Extra: Understanding package.json
- Updating a Package with npm
- Setting Up a Local Test Server
- Enforcing Code Standards with a Linter
- Styling Code Consistently with a Formatter
- Experimenting in a JavaScript Playground
- 2. Strings and Regular Expressions
- Checking for an Existing, Nonempty String
- Converting a Numeric Value to a Formatted String
- Inserting Special Characters
- Inserting Emojis
- Using Template Literals for Clearer String Concatenation
- Performing a Case-Insensitive String Comparison
- Checking If a String Contains a Specific Substring
- Replacing All Occurrences of a String
- Replacing HTML Tags with Named Entities
- Using a Regular Expression to Replace Patterns in a String
- Extra: Regular Expressions
- Extracting a List from a String
- Finding All Instances of a Pattern
- Extra: Highlighting Matches
- Removing Whitespace from the Beginning and End of a String
- Converting the First Letter of a String to Uppercase
- Validating an Email Address
- 3. Numbers
- Generating Random Numbers
- Generating Cryptographically Secure Random Numbers
- Rounding to a Specific Decimal Place
- Preserving Accuracy in Decimal Values
- Converting a String to a Number
- Converting a Decimal to a Hexadecimal Value
- Converting Between Degrees and Radians
- Calculating the Length of a Circular Arc
- Manipulating Very Large Numbers with BigInt
- 4. Dates
- Getting the Current Date and Time
- Converting a String to a Date
- Adding Days to a Date
- Comparing Dates and Testing Dates for Equality
- Calculating the Time Elapsed Between Two Dates
- Formatting a Date Value as a String
- 5. Arrays
- Checking If an Object Is an Array
- Iterating Over All the Elements in an Array
- Checking If Two Arrays Are Equal
- Breaking Down an Array into Separate Variables
- Passing an Array to a Function That Expects a List of Values
- Cloning an Array
- Merging Two Arrays
- Copying a Portion of an Array by Position
- Extracting Array Items That Meet Specific Criteria
- Emptying an Array
- Removing Duplicate Values
- Flattening a Two-Dimensional Array
- Searching Through an Array for Exact Matches
- Searching Through an Array for Items That Meet Specific Criteria
- Removing or Replacing Array Elements
- Sorting an Array of Objects by a Property Value
- Transforming Every Element of an Array
- Combining an Arrayâs Values in a Single Calculation
- Validating Array Contents
- Creating a Collection of Nonduplicated Values
- Creating a Key-Indexed Collection of Items
- 6. Functions
- Passing a Function as an Argument to Another Function
- Using Arrow Functions
- Providing a Default Parameter Value
- Creating a Function That Accepts Unlimited Arguments
- Using Named Function Parameters
- Creating a Function That Stores its State with a Closure
- Creating a Generator Function That Yields Multiple Values
- Extra: Building a Repeatable Pseudorandom Number Generator
- Reducing Redundancy by Using Partial Application
- Advanced: A Partial Function Factory
- Extra: Using bind() to Partially Provide Arguments
- Fixing this with Function Binding
- Extra: self = this
- Implementing a Recursive Algorithm
- 7. Objects
- Checking if an Object Is a Certain Type
- Using an Object Literal to Bundle Data
- Extra: Computed Property Names
- Checking If an Object Has a Property
- Iterating Over All the Properties of an Object
- Testing for an Empty Object
- Merging the Properties of Two Objects
- Customizing the Way a Property Is Defined
- Preventing Any Changes to an Object
- Intercepting and Changing Actions on an Object with a Proxy
- Cloning an Object
- Making a Deep Copy of an Object
- Creating Absolutely Unique Object Property Keys
- Creating Enums with Symbol
- 8. Classes
- Creating a Reusable Class
- Extra: Multiple Constructors
- Adding Properties to a Class
- Extra: Private Fields
- Giving a Class a Better String Representation
- Using the Constructor Pattern to Make a Custom Class
- Supporting Method Chaining in Your Class
- Adding Static Methods to a Class
- Using a Static Method to Create Objects
- Inheriting Functionality from Another Class
- Extra: Prototype Chains
- Organizing Your JavaScript Classes with Modules
- 9. Asynchronous Programming
- Updating the Page During a Loop
- Using a Function That Returns a Promise
- Promisifying an Asynchronous Function That Uses a Callback
- Executing Multiple Promises Concurrently
- Waiting for a Promise to Finish with Await and Async
- Creating an Asynchronous Generator Function
- Using a Web Worker to Perform a Background Task
- Adding Progress Support to a Web Worker
- 10. Errors and Testing
- Catching and Neutralizing an Error
- Catching Different Types of Errors
- Catching Asynchronous Errors
- Detecting Unhandled Errors
- Extra: Logging Tools
- Throwing a Standard Error
- Throwing a Custom Error
- Writing Unit Tests for Your Code
- Extra: Writing Tests First
- Tracking Test Code Coverage
- II. JavaScript in the Browser
- 11. Browser Tools
- Debugging JavaScript
- Analyzing Runtime Performance
- Identifying Unused JavaScript
- Using Lighthouse to Measure Best Practices
- 12. Working with HTML
- Accessing a Given Element and Finding Its Parent and Child Elements
- Traversing the Results from querySelectorAll() with forEach()
- Adding Click Functionality to an Element
- Finding All Elements That Share an Attribute
- Accessing All Elements of a Specific Type
- Discovering Child Elements Using the Selectors API
- Changing an Element`s Class Value
- Setting an Element`s Style Attribute
- Extra: Accessing an Existing Style Setting
- Advanced
- Adding Text to a New Paragraph
- Inserting a New Element in a Specific DOM Location
- Checking If a Checkbox Is Checked
- Adding Up Values in an HTML Table
- Extra: forEach and querySelectorAll
- Extra: Modularization of Globals
- Deleting Rows from an HTML Table
- Hiding Page Sections
- Creating Hover-Based Pop-Up Info Windows
- Validating Form Data
- Extra: HTML5 Form Validation Techniques
- Highlighting Form Errors and Accessibility
- Creating an Accessible Automatically Updated Region
- 13. Fetching Remote Data
- Requesting Remote Data with Fetch
- Using XMLHttpRequest
- Submitting a Form
- Populating a Selection List from the Server
- Parsing Returned JSON
- Fetching and Parsing XML
- Sending Binary Data and Loading into an Image
- Sharing HTTP Cookies Across Domains
- Using Websockets to Establish a Two-Way Communication Between Client and Server
- Long Polling a Remote Data Source
- 14. Data Persistence
- Persisting Information with Cookies
- Using sessionStorage for Client-Side Storage
- Creating a localStorage Client-Side Data Storage Item
- Persisting Larger Chunks of Data on the Client Using IndexedDB
- Simplifying IndexedDB with a Library
- 15. Working with Media
- Adding JavaScript to SVG
- Extra: Using SVG Libraries
- Accessing SVG from a Web Page Script
- Creating an SVG Bar Chart with D3
- Integrating SVG and the Canvas Element in HTML
- Extra: Canvas? Or SVG?
- Running a Routine When an Audio File Begins Playing
- Controlling Video from JavaScript with the video Element
- 16. Writing Web Applications
- Bundling JavaScript
- Extra: Using npm Modules
- JavaScript and the Mobile Web
- Writing a Progressive Web Application
- Testing and Profiling a Progressive Web Application
- Getting the Value of the Current URL
- Redirecting a URL
- Copying Text to a User`s Clipboard
- Enabling a Mobile-Like Notification in the Desktop Browser
- Extra: Web Notifications and the Page Visibility API
- Loading a File Locally in the Browser
- Extending the Possible with Web Components
- Choosing a Front-End Framework
- React
- Vue
- Svelte
- Angular
- III. Node.js
- 17. Node Basics
- Managing Node Versions with Node Version Manager
- Responding to a Simple Browser Request
- Interactively Trying Out Node Code Snippets with REPL
- Extra: Wait a Second, What Global Object?
- Reading and Writing File Data
- Getting Input from the Terminal
- Getting the Path to the Current Script
- Working with Node Timers and Understanding the Node Event Loop
- 18. Node Modules
- Searching for a Specific Node Module via npm
- Converting Your Library into a Node Module
- Taking Your Code Across Module Environments
- Creating an Installable Node Module
- Extra: The README File and Markdown Syntax
- Writing Multiplatform Libraries
- Unit Testing Your Modules
- 19. Managing Node
- Using Environment Variables
- Managing Callback Hell
- Accessing Command-Line Functionality Within a Node Application
- Extra: Using Child Processes with Windows
- Passing Command-Line Arguments
- Creating a Command-Line Utility with Help from Commander
- Keeping a Node Instance Up and Running
- Monitoring Application Changes and Restarting During Local Development
- Scheduling Repeat Tasks
- Testing the Performance and Capability of Your WebSockets Application
- 20. Remote Data
- Fetching Remote Data
- Screen Scraping
- Accessing JSON-Formatted Data via a RESTful API
- 21. Building Web Applications with Express
- Using Express to Respond to Requests
- Using the Express-Generator
- Routing
- Working with OAuth
- OAuth 2 User Authentication with Passport.js
- Serving Up Formatted Data
- Building a RESTful API
- Building a GraphQL API
- Index