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.
Publisher resources
Table of contents
- Title Page
- Copyright and Credits
- Dedication
- PacktPub.com
- Contributors
- Preface
-
Building with Modules
- Introduction
- Installing and configuring - Chrome
- Installing and configuring - Firefox
- Installing Python, using SimpleHTTPServer to host a local static file server
- Creating an HTML page that loads an ECMAScript module
- Exporting/importing multiple modules for external use
- Renaming imported modules
- Nesting modules under a single namespace
-
Staying Compatible with Legacy Browsers
- Introduction
- Installing Node.js with NVM – Linux and macOS
- Installing Node.js with NVM: Windows
- Installing and configuring webpack
- Adding fallback script tags to load client bundles
- Shimming methods with Babel Polyfill
- Supporting new language features with Babel
- Using tools to analyze webpack bundles
- Working with Promises
-
Working with async/await and Functions
- Introduction
- Creating promises with an async function
- Awaiting the result of async functions
- Using async results in a Promise chain
- Awaiting multiple results in a series
- Awaiting multiple results concurrently
- Using Promise.all to collect an array of async results
- Handling errors when awaiting an async function
- Handling errors thrown inside of Promise.all
- Using finally to ensure an operation is performed
-
Web Workers, Shared Memory, and Atomics
- Introduction
- Enabling SharedArrayBuffers in Firefox
- Enabling SharedArrayBuffers in Chrome
- Performing work on separate threads with Web Workers
- Sending messages to and from Web Workers
- Sending data to a Web Worker
- Stopping workers with terminate
- Creating SharedArrayBuffer
- Sending SharedArrayBuffer to a Web Worker
- Reading SharedArray from multiple Web Workers
- Using Atomics to coordinate the use of shared memory
- Using promises to provide a simple interface for a worker
-
Plain Objects
- Introduction
- Using Object.assign to add properties to an object
- Using Object.entries to get iterable property-name pairs
- Using Object.is to compare two values
- Defining function properties as methods on a plain object
- Defining read-only props with Object.defineProperty
- Overriding read-only props with Object.defineProperty
- Creating a non-enumerable property with Object.defineProperty
- Creating an object using object structuring
- Picking values from an object using destructuring
- Using a spread operator to combine objects
- Creating Classes
-
Inheritance and Composition
- Introduction
- Extending a class
- Assigning additional properties with constructor arguments
- Overriding parent class instance methods
- Overriding parent class static methods
- Calling super methods
- Using composition rather than inheritance to add behavior
- Using mix-ins to add behavior
- Passing a class as an argument
- Checking class inheritance with Object.getPrototypeOf
- Using throw to simulate abstract classes
-
Larger Structures with Design Patterns
- Introduction
- Defining steps with template functions
- Assembling customized instances with builders
- Replicating instances with factories
- Processing a structure with the visitor pattern
- Using a singleton to manage instances
- Modifying an existing design pattern to fit different use cases
- Combining existing design patterns to fit different use cases
-
Working with Arrays
- Introduction
- Using Array#find and Array#filter to find values in an array
- Using Array#slice to get a subset of the array
- Using Array#every and Array#some to test array values
- Using Array.map to produce values
- Using Array.reduce to transform data
- Extracting array members with destructuring
- Getting the head and tail of array using the rest operator
- Combining arrays with the spread operator
-
Working with Maps and Symbols
- Introduction
- Using Symbol to create a local instance
- Using Symbol.for to create a global instance
- Using Symbol to simulate enums
- Setting and deleting entries from Map
- Creating a Map from the existing data
- Creating a class that wraps Map to work with specific complex types
- Setting and deleting entries from WeakMap
- Creating a WeakMap from existing data  
- Creating a class that uses WeakMap to work with specific complex types
-
Working with Sets
- Introduction
- Adding and deleting items from a Set
- Creating a Set from existing data
- Adding and deleting items from WeakSet
- Creating a WeakSet from existing data
- Finding the union of two sets
- Finding the intersection of two sets
- Finding the difference between two sets
- Creating a class that wraps a Set to work with more complex types
- Other Books You May Enjoy
Product information
- Title: ECMAScript Cookbook
- Author(s):
- Release date: March 2018
- Publisher(s): Packt Publishing
- ISBN: 9781788628174
You might also like
book
Understanding ECMAScript 6
Understanding ECMAScript 6 is your roadmap to the new object types, syntax, and other exciting features …
book
Practical Modern JavaScript
To get the most out of modern JavaScript, you need learn the latest features of its …
book
Learn ECMAScript - Second Edition
Get up and running with all the new features of ECMAScript and explore new ways of …
book
JavaScript
All of JavaScript's newest features, in depth, made easy to understand. JavaScript is a rapidly changing …