JavaScript Cookbook, 3rd Edition Год издания: 2021 Автор: Scott A.D., MacDonald M., Powers S. Издательство: O'Reilly ISBN: 978-1-492-05575-4 Язык: Английский Формат: PDF/ePub Качество: Издательский макет или текст (eBook) Интерактивное оглавление: Да Количество страниц: 538 Описание: Why reinvent the wheel every time you run into a problem with JavaScript? This cookbook is chock-full of code recipes for common programming tasks, along with techniques for building apps that work in any browser. You'll get adaptable code samples that you can add to almost any project--and you'll learn more about JavaScript in the process. The recipes in this book take advantage of the latest features in ECMAScript 2020 and beyond and use modern JavaScript coding standards. You'll learn how to:
Set up a productive development environment with a code editor, linter, and test server
Work with JavaScript data types, such as strings, arrays, and BigInts
Improve your understanding of JavaScript functions, including arrow functions, closures, and generators
Apply object-oriented programming concepts like classes and inheritance
Work with rich media in JavaScript, including audio, video, and SVGs
Manipulate HTML markup and CSS styles
Use JavaScript anywhere with Node.js
Access and manipulate remote data with REST, GraphQL, and Fetch
Get started with the popular Express application-building framework
Perform asynchronous operations with Promises, async/await, and web workers
Примеры страниц
Оглавление
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Part I. The JavaScript Language 1. Setting Up a Development Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Choosing a Code Editor 2 1.2 Using the Developer Console in Your Browser 3 1.3 Running Blocks of Code in the Developer Console 7 1.4 Using Strict Mode to Catch Common Mistakes 9 1.5 Filling in HTML Boilerplate with Emmet Shortcuts 11 1.6 Installing the npm Package Manager (with Node.js) 13 1.7 Downloading a Package with npm 16 1.8 Updating a Package with npm 20 1.9 Setting Up a Local Test Server 21 1.10 Enforcing Code Standards with a Linter 24 1.11 Styling Code Consistently with a Formatter 28 1.12 Experimenting in a JavaScript Playground 31 2. Strings and Regular Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.1 Checking for an Existing, Nonempty String 35 2.2 Converting a Numeric Value to a Formatted String 38 2.3 Inserting Special Characters 40 2.4 Inserting Emojis 42 2.5 Using Template Literals for Clearer String Concatenation 43 2.6 Performing a Case-Insensitive String Comparison 45 2.7 Checking If a String Contains a Specific Substring 46 2.8 Replacing All Occurrences of a String 47 2.9 Replacing HTML Tags with Named Entities 48 2.10 Using a Regular Expression to Replace Patterns in a String 49 2.11 Extracting a List from a String 52 2.12 Finding All Instances of a Pattern 54 2.13 Removing Whitespace from the Beginning and End of a String 57 2.14 Converting the First Letter of a String to Uppercase 58 2.15 Validating an Email Address 59 3. Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.1 Generating Random Numbers 61 3.2 Generating Cryptographically Secure Random Numbers 63 3.3 Rounding to a Specific Decimal Place 65 3.4 Preserving Accuracy in Decimal Values 66 3.5 Converting a String to a Number 68 3.6 Converting a Decimal to a Hexadecimal Value 70 3.7 Converting Between Degrees and Radians 71 3.8 Calculating the Length of a Circular Arc 71 3.9 Manipulating Very Large Numbers with BigInt 72 4. Dates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 4.1 Getting the Current Date and Time 75 4.2 Converting a String to a Date 77 4.3 Adding Days to a Date 79 4.4 Comparing Dates and Testing Dates for Equality 80 4.5 Calculating the Time Elapsed Between Two Dates 82 4.6 Formatting a Date Value as a String 84 5. Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 5.1 Checking If an Object Is an Array 88 5.2 Iterating Over All the Elements in an Array 88 5.3 Checking If Two Arrays Are Equal 90 5.4 Breaking Down an Array into Separate Variables 93 5.5 Passing an Array to a Function That Expects a List of Values 94 5.6 Cloning an Array 95 5.7 Merging Two Arrays 97 5.8 Copying a Portion of an Array by Position 98 5.9 Extracting Array Items That Meet Specific Criteria 100 5.10 Emptying an Array 101 5.11 Removing Duplicate Values 102 5.12 Flattening a Two-Dimensional Array 103 5.13 Searching Through an Array for Exact Matches 104 5.14 Searching Through an Array for Items That Meet Specific Criteria 105 5.15 Removing or Replacing Array Elements 107 5.16 Sorting an Array of Objects by a Property Value 108 5.17 Transforming Every Element of an Array 109 5.18 Combining an Array’s Values in a Single Calculation 110 5.19 Validating Array Contents 112 5.20 Creating a Collection of Nonduplicated Values 113 5.21 Creating a Key-Indexed Collection of Items 114 6. Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 6.1 Passing a Function as an Argument to Another Function 117 6.2 Using Arrow Functions 121 6.3 Providing a Default Parameter Value 124 6.4 Creating a Function That Accepts Unlimited Arguments 125 6.5 Using Named Function Parameters 126 6.6 Creating a Function That Stores its State with a Closure 129 6.7 Creating a Generator Function That Yields Multiple Values 131 6.8 Reducing Redundancy by Using Partial Application 135 6.9 Fixing this with Function Binding 138 6.10 Implementing a Recursive Algorithm 141 7. Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 7.1 Checking if an Object Is a Certain Type 145 7.2 Using an Object Literal to Bundle Data 147 7.3 Checking If an Object Has a Property 150 7.4 Iterating Over All the Properties of an Object 152 7.5 Testing for an Empty Object 154 7.6 Merging the Properties of Two Objects 155 7.7 Customizing the Way a Property Is Defined 156 7.8 Preventing Any Changes to an Object 159 7.9 Intercepting and Changing Actions on an Object with a Proxy 161 7.10 Cloning an Object 164 7.11 Making a Deep Copy of an Object 166 7.12 Creating Absolutely Unique Object Property Keys 168 7.13 Creating Enums with Symbol 170 8. Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 8.1 Creating a Reusable Class 173 8.2 Adding Properties to a Class 177 8.3 Giving a Class a Better String Representation 182 8.4 Using the Constructor Pattern to Make a Custom Class 183 8.5 Supporting Method Chaining in Your Class 186 8.6 Adding Static Methods to a Class 188 8.7 Using a Static Method to Create Objects 190 8.8 Inheriting Functionality from Another Class 192 8.9 Organizing Your JavaScript Classes with Modules 197 9. Asynchronous Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 9.1 Updating the Page During a Loop 202 9.2 Using a Function That Returns a Promise 204 9.3 Promisifying an Asynchronous Function That Uses a Callback 208 9.4 Executing Multiple Promises Concurrently 211 9.5 Waiting for a Promise to Finish with Await and Async 214 9.6 Creating an Asynchronous Generator Function 218 9.7 Using a Web Worker to Perform a Background Task 220 9.8 Adding Progress Support to a Web Worker 224 10. Errors and Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 10.1 Catching and Neutralizing an Error 227 10.2 Catching Different Types of Errors 230 10.3 Catching Asynchronous Errors 232 10.4 Detecting Unhandled Errors 233 10.5 Throwing a Standard Error 237 10.6 Throwing a Custom Error 239 10.7 Writing Unit Tests for Your Code 241 10.8 Tracking Test Code Coverage 247 Part II. JavaScript in the Browser 11. Browser Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 11.1 Debugging JavaScript 253 11.2 Analyzing Runtime Performance 255 11.3 Identifying Unused JavaScript 257 11.4 Using Lighthouse to Measure Best Practices 259 12. Working with HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 12.1 Accessing a Given Element and Finding Its Parent and Child Elements 263 12.2 Traversing the Results from querySelectorAll() with forEach() 266 12.3 Adding Click Functionality to an Element 267 12.4 Finding All Elements That Share an Attribute 269 12.5 Accessing All Elements of a Specific Type 269 12.6 Discovering Child Elements Using the Selectors API 272 12.7 Changing an Element’s Class Value 273 12.8 Setting an Element’s Style Attribute 274 12.9 Adding Text to a New Paragraph 276 12.10 Inserting a New Element in a Specific DOM Location 278 12.11 Checking If a Checkbox Is Checked 279 12.12 Adding Up Values in an HTML Table 280 12.13 Deleting Rows from an HTML Table 283 12.14 Hiding Page Sections 285 12.15 Creating Hover-Based Pop-Up Info Windows 287 12.16 Validating Form Data 289 12.17 Highlighting Form Errors and Accessibility 292 12.18 Creating an Accessible Automatically Updated Region 298 13. Fetching Remote Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 13.1 Requesting Remote Data with Fetch 301 13.2 Using XMLHttpRequest 305 13.3 Submitting a Form 306 13.4 Populating a Selection List from the Server 310 13.5 Parsing Returned JSON 314 13.6 Fetching and Parsing XML 316 13.7 Sending Binary Data and Loading into an Image 318 13.8 Sharing HTTP Cookies Across Domains 319 13.9 Using Websockets to Establish a Two-Way Communication Between Client and Server 320 13.10 Long Polling a Remote Data Source 322 14. Data Persistence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 14.1 Persisting Information with Cookies 325 14.2 Using sessionStorage for Client-Side Storage 328 14.3 Creating a localStorage Client-Side Data Storage Item 334 14.4 Persisting Larger Chunks of Data on the Client Using IndexedDB 338 14.5 Simplifying IndexedDB with a Library 341 15. Working with Media. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 15.1 Adding JavaScript to SVG 345 15.2 Accessing SVG from a Web Page Script 348 15.3 Creating an SVG Bar Chart with D3 350 15.4 Integrating SVG and the Canvas Element in HTML 354 15.5 Running a Routine When an Audio File Begins Playing 356 15.6 Controlling Video from JavaScript with the video Element 357 16. Writing Web Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 16.1 Bundling JavaScript 361 16.2 JavaScript and the Mobile Web 363 16.3 Writing a Progressive Web Application 366 16.4 Testing and Profiling a Progressive Web Application 373 16.5 Getting the Value of the Current URL 377 16.6 Redirecting a URL 379 16.7 Copying Text to a User’s Clipboard 380 16.8 Enabling a Mobile-Like Notification in the Desktop Browser 382 16.9 Loading a File Locally in the Browser 385 16.10 Extending the Possible with Web Components 388 16.11 Choosing a Front-End Framework 391 Part III. Node.js 17. Node Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 17.1 Managing Node Versions with Node Version Manager 397 17.2 Responding to a Simple Browser Request 400 17.3 Interactively Trying Out Node Code Snippets with REPL 402 17.4 Reading and Writing File Data 405 17.5 Getting Input from the Terminal 410 17.6 Getting the Path to the Current Script 412 17.7 Working with Node Timers and Understanding the Node Event Loop 413 18. Node Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 18.1 Searching for a Specific Node Module via npm 420 18.2 Converting Your Library into a Node Module 421 18.3 Taking Your Code Across Module Environments 422 18.4 Creating an Installable Node Module 425 18.5 Writing Multiplatform Libraries 431 18.6 Unit Testing Your Modules 435 19. Managing Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439 19.1 Using Environment Variables 439 19.2 Managing Callback Hell 441 19.3 Accessing Command-Line Functionality Within a Node Application 444 19.4 Passing Command-Line Arguments 447 19.5 Creating a Command-Line Utility with Help from Commander 448 19.6 Keeping a Node Instance Up and Running 450 19.7 Monitoring Application Changes and Restarting During Local Development 452 19.8 Scheduling Repeat Tasks 453 19.9 Testing the Performance and Capability of Your WebSockets Application 455 20. Remote Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 20.1 Fetching Remote Data 457 20.2 Screen Scraping 459 20.3 Accessing JSON-Formatted Data via a RESTful API 461 21. Building Web Applications with Express. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 21.1 Using Express to Respond to Requests 465 21.2 Using the Express-Generator 469 21.3 Routing 474 21.4 Working with OAuth 476 21.5 OAuth 2 User Authentication with Passport.js 486 21.6 Serving Up Formatted Data 491 21.7 Building a RESTful API 492 21.8 Building a GraphQL API 496 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum