ECMAScript Cookbook

Book description

Become a better web programmer by writing efficient and modular code using ES6 and ES8

About This Book

  • Learn to write asynchronous code and improve the readability of your web applications
  • Explore advanced concepts such as closures, Proxy, generators, Promise, async functions, and Atomics
  • Use different design patterns to create structures to solve common organizational and processing issues

Who This Book Is For

If you're a web developer with a basic understanding of JavaScript and wish to learn the latest features of ECMAScript for developing efficient web applications, this book is for you.

What You Will Learn

  • Organize JavaScript programs across multiple files, using ES modules
  • Create and work with promises using the Promise object and methods
  • Compose async functions to propagate and handle errors
  • Solve organizational and processing issues with structures using design patterns
  • Use classes to encapsulate and share behavior
  • Orchestrate parallel programs using WebWorkers, SharedMemory, and Atomics
  • Use and extend Map, Set, and Symbol to work with user-defined classes and simulate data types
  • Explore new array methods to avoid looping with arrays and other collections

In Detail

ECMAScript Cookbook follows a modular approach with independent recipes covering different feature sets and specifications of ECMAScript to help you become an efficient programmer.

This book starts off with organizing your JavaScript applications as well as delivering those applications to modem and legacy systems. You will get acquainted with features of ECMAScript 8 such as async, SharedArrayBuffers, and Atomic operations that enhance asynchronous and parallel operations. In addition to this, this book will introduce you to SharedArrayBuffers, which allow web workers to share data directly, and Atomic operations, which help coordinate behavior across the threads. You will also work with OOP and Collections, followed by new functions and methods on the built-in Object and Array types that make common operations more manageable and less error-prone. You will then see how to easily build more sophisticated and expressive program structures with classes and inheritance. In the end, we will cover Sets, Maps, and Symbols, which are the new types introduced in ECMAScript 6 to add new behaviors and allow you to create simple and powerful modules.

By the end of the book, you will be able to produce more efficient, expressive, and simpler programs using the new features of ECMAScript.

Style and approach

This book will follow a modular approach covering independent recipes on different features of ECMAScript throughout the book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. ECMAScript Cookbook
  3. Dedication
  4. PacktPub.com
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Get in touch
      1. Reviews
  7. Building with Modules
    1. Introduction
    2. Installing and configuring - Chrome
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Installing and configuring - Firefox
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Installing Python, using SimpleHTTPServer to host a local static file server
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating an HTML page that loads an ECMAScript module
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Exporting/importing multiple modules for external use
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Renaming imported modules
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Nesting modules under a single namespace
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
  8. Staying Compatible with Legacy Browsers
    1. Introduction
    2. Installing Node.js with NVM – Linux and macOS
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Installing Node.js with NVM: Windows
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Installing and configuring webpack
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Adding fallback script tags to load client bundles
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Shimming methods with Babel Polyfill
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Supporting new language features with Babel
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    8. Using tools to analyze webpack bundles
      1. Getting ready
      2. How to do it...
      3. How it works...
  9. Working with Promises
    1. Introduction
    2. Creating and waiting for Promises
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Resolving Promise results
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Rejecting Promise errors
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Chaining Promises
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Starting a Promise chain with Promise.resolve
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using Promise.all to resolve multiple promises
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more
    8. Handling errors with Promise.catch
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Simulating finally with the promise API
      1. Getting ready
      2. How to do it...
      3. How it works...
  10. Working with async/await and Functions
    1. Introduction
    2. Creating promises with an async function
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Awaiting the result of async functions
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using async results in a Promise chain
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Awaiting multiple results in a series
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Awaiting multiple results concurrently
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using Promise.all to collect an array of async results
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Handling errors when awaiting an async function
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Handling errors thrown inside of Promise.all
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Using finally to ensure an operation is performed
      1. Getting ready
      2. How to do it...
      3. How it works...
  11. Web Workers, Shared Memory, and Atomics
    1. Introduction
    2. Enabling SharedArrayBuffers in Firefox
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Enabling SharedArrayBuffers in Chrome
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Performing work on separate threads with Web Workers
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Sending messages to and from Web Workers
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Sending data to a Web Worker
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Stopping workers with terminate
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Creating SharedArrayBuffer
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Sending SharedArrayBuffer to a Web Worker
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Reading SharedArray from multiple Web Workers
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Using Atomics to coordinate the use of shared memory
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Using promises to provide a simple interface for a worker
      1. Getting ready
      2. How to do it...
      3. How it works...
  12. Plain Objects
    1. Introduction
    2. Using Object.assign to add properties to an object
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using Object.entries to get iterable property-name pairs
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using Object.is to compare two values
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Defining function properties as methods on a plain object
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Defining read-only props with Object.defineProperty
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Overriding read-only props with Object.defineProperty
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Creating a non-enumerable property with Object.defineProperty
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Creating an object using object structuring
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Picking values from an object using destructuring
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Using a spread operator to combine objects
      1. Getting ready
      2. How to do it...
      3. How it works...
  13. Creating Classes
    1. Introduction
    2. Creating a new class
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Assigning properties with constructor arguments
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Defining methods on a class
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Checking instance type with instanceOf
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Using getters to create read-only properties
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using setters to encapsulate values
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Using static methods to work with all instances
      1. Getting ready
      2. How to do it...
      3. How it works...
  14. Inheritance and Composition
    1. Introduction
    2. Extending a class
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Assigning additional properties with constructor arguments
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Overriding parent class instance methods
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Overriding parent class static methods
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Calling super methods
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using composition rather than inheritance to add behavior
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Using mix-ins to add behavior
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Passing a class as an argument
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Checking class inheritance with Object.getPrototypeOf
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Using throw to simulate abstract classes
      1. Getting ready
      2. How to do it...
      3. How it works...
  15. Larger Structures with Design Patterns
    1. Introduction
    2. Defining steps with template functions
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Assembling customized instances with builders
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Replicating instances with factories
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Processing a structure with the visitor pattern
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Using a singleton to manage instances
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Modifying an existing design pattern to fit different use cases
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Combining existing design patterns to fit different use cases
      1. Getting ready
      2. How to do it...
      3. How it works...
  16. Working with Arrays
    1. Introduction
    2. Using Array#find and Array#filter to find values in an array
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using Array#slice to get a subset of the array
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using Array#every and Array#some to test array values
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using Array.map to produce values
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Using Array.reduce to transform data
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Extracting array members with destructuring
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Getting the head and tail of array using the rest operator
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Combining arrays with the spread operator
      1. Getting ready
      2. How to do it...
      3. How it works...
  17. Working with Maps and Symbols
    1. Introduction
    2. Using Symbol to create a local instance
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using Symbol.for to create a global instance
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using Symbol to simulate enums
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Setting and deleting entries from Map
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Creating a Map from the existing data
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Creating a class that wraps Map to work with specific complex types
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Setting and deleting entries from WeakMap
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Creating a WeakMap from existing data  
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Creating a class that uses WeakMap to work with specific complex types
      1. Getting ready
      2. How to do it...
      3. How it works...
  18. Working with Sets
    1. Introduction
    2. Adding and deleting items from a Set
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Creating a Set from existing data
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Adding and deleting items from WeakSet
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating a WeakSet from existing data
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Finding the union of two sets
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Finding the intersection of two sets
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Finding the difference between two sets
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Creating a class that wraps a Set to work with more complex types
      1. Getting ready
      2. How to do it...
      3. How it works...
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: ECMAScript Cookbook
  • Author(s): Ross Harrison
  • Release date: March 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788628174