Learn ECMAScript - Second Edition

Book description

Get up and running with all the new features of ECMAScript and explore new ways of coding with JavaScript.

About This Book

  • Grasp the latest features of ECMAScript and the best way to use it in production code
  • Learn newly added native APIs to JS Engine and perform tasks efficiently with a cleaner code base
  • Understand the more complex sides of JavaScript such as the inheritance model, low-level memory management, multithreaded environments, and web workers

Who This Book Is For

This book is for web developers who have some basic programming knowledge and want to learn to write cleaner code with the power of ECMAScript.

What You Will Learn

  • Implement methods associated with objects as per the latest ECMAScript specification
  • Make use of the latest features of ECMAScript
  • Make use of many new APIs in HTML5 and modern JavaScript implementation
  • Use SharedArrayBuffers for superfast concurrent and parallel programming
  • Perform asynchronous programming with JavaScript
  • Implement the best ways and practices to perform modular programming in JavaScript

In Detail

Learn ECMAScript explores implementation of the latest ECMAScript features to add to your developer toolbox, helping you to progress to an advanced level. Learn to add 1 to a variable andsafely access shared memory data within multiple threads to avoid race conditions.

You'll start the book by building on your existing knowledge of JavaScript, covering performing arithmetic operations, using arrow functions and dealing with closures. Next, you will grasp the most commonly used ECMAScript skills such as reflection, proxies, and classes. Furthermore, you'll learn modularizing the JS code base, implementing JS on the web and how the modern HTML5 + JS APIs provide power to developers on the web. Finally, you will learn the deeper parts of the language, which include making JavaScript multithreaded with dedicated and shared web workers, memory management, shared memory, and atomics. It doesn't end here; this book is 100% compatible with ES.Next.

By the end of this book, you'll have fully mastered all the features of ECMAScript!

Style and approach

The level goes gradually from basic to advanced so that the reader can adapt at every point and level up their skills at the same time. The chapters are carefully arranged in a manner that makes every concept easy to learn and deploy right away in your code.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learn ECMAScript Second Edition
  3. PacktPub.com
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. 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. Get in touch
      1. Reviews
  6. Getting Started with ECMAScript
    1. The let keyword
      1. Declaring function-scoped variables
      2. Declaring block-scoped variables
      3. Re-declaring variables
      4. Closures and let keyword
    2. The const keyword
      1. The scope of const variables
      2. Referencing objects using constant variables
      3. When to use var/let/const 
      4. Let versus var versus const performance benchmarks
    3. Immutability in JavaScript
      1. Object.freeze versus const
    4. Default parameter values
    5. The spread operator
      1. Other uses of the spread operator
        1. Making array values a part of another array
        2. Pushing the values of an array into another array
      2. Spreading multiple arrays
    6. The rest parameter
    7. Hoisting
    8. Destructuring assignments
      1. The array destructuring assignment
        1. Ignoring values
        2. Using the rest operator in an array destructuring assignment
        3. Default values for variables
        4. Nested array destructuring
        5. Using a destructuring assignment as a parameter
      2. Object destructuring assignments
        1. Default values for variables
        2. Destructuring nested objects
        3. Using the object destructuring assignment as a parameter
    9. Arrow functions
      1. The value of "this" in an arrow function
      2. Other differences between arrow and traditional functions
    10. Enhanced object literals
      1. Defining properties
      2. Defining methods
      3. Computed property names
    11. Trailing commas and JavaScript
    12. The semicolon dilemma
      1. Automatic semicolon insertion in JavaScript
      2. Where to insert semicolons in JavaScript?
    13. Summary
  7. Knowing Your Library
    1. Working with numbers
      1. The binary notation
      2. The octal notation
      3. The Number.isInteger(number) method
      4. The Number.isNaN(value) method
      5. isNaN versus Number.isNaN
      6. The Number.isFinite(number) method
      7. The Number.isSafeInteger(number) method
      8. The Number.EPSILON property
    2. Doing math
      1. Trigonometry-related operations
      2. Arithmetic-related operations
      3. Exponential operator
      4. Miscellaneous math methods
        1. The Math.imul(number1, number2) function
        2. The Math.clz32(number) function
        3. The Math.sign(number) function
        4. The Math.trunc(number) function
        5. The Math.fround(number) function
      5. Working with strings
        1. The repeat(count) method
        2. The includes(string, index) method
        3. The startsWith(string, index) method
        4. The endsWith(string, index) function
        5. The indexOf(string) function
        6. The lastIndexOf(string)
        7. The padStart(length [, padString])
        8. The padEnd(length [, padString])
      6. Template strings
      7. Expressions
        1. Tagged template literals
        2. Multiline strings
        3. Raw strings
        4. Escape sequence problem with template literals
    3. Arrays
      1. The Array.from(iterable, mapFunc, this) method
      2. The Array.of(values…) method
      3. The fill(value, startIndex, endIndex) method
      4. The includes() method
      5. The includes() versus the indexOf() method
      6. The find(testingFunc) method
      7. The findIndex(testingFunc) method
      8. The copyWithin(targetIndex, startIndex, endIndex) function
      9. The entries(), keys(), and values() methods
      10. Array iteration
        1. The map() method
        2. The filter() method
        3. forEach() method
        4. some() method
    4. Collections
      1. ArrayBuffer
      2. Typed arrays
      3. Set
      4. WeakSet
      5. Map
      6. WeakMap
    5. Objects
      1. Object.values()
      2. Object.entries()
      3. The __proto__ property
      4. The Object.is(value1, value2) method
      5. The Object.setPrototypeOf(object, prototype) method
      6. The Object.assign(targetObj, sourceObjs…) method
      7. Object.getOwnPropertyDescriptors()
    6. Summary
  8. Using Iterators
    1. Symbols – primitive data type
      1. The typeof operator
      2. The new operator
      3. Using symbols as the object property keys
      4. The Object.getOwnPropertySymbols() method
      5. The Symbol.for(string) method
      6. Well-known symbols
    2. The iteration protocol
      1. The iterator protocol
      2. The iterable protocol
    3. Generator function
      1. The return(value) method
      2. The throw(exception) method
      3. The yield* keyword
      4. The for…of loop
    4. Tail call optimization
      1. Why tail call optimization?
      2. Converting non-tail calls into tail calls
    5. Summary
  9. Asynchronous Programming
    1. JavaScript execution model
    2. The event loop
      1. The call stack
      2. Stack, queue, and Web APIs
    3. Writing asynchronous code
      1. Asynchronous code involving events
      2. Asynchronous code involving callbacks
    4. Promises and async programming
      1. Promise states
      2. Promises versus callbacks
      3. Promise constructor and (resolve, reject) methods
      4. The then(onFulfilled, onRejected) method
      5. The catch(onRejected) method
      6. The Promise.resolve(value) method
      7. The Promise.reject(value) method
      8. The Promise.all(iterable) method
      9. The Promise.race(iterable) method
    5. async/await – the future of asynchronous programming
      1. async/await versus promises
      2. The async function and await keyword
      3. Making asynchronous code look synchronous
    6. Summary
  10. Modular Programming
    1. JavaScript modules 101
    2. Implementing modules – the old way
      1. Immediately-Invoked Function Expression (IIFE)
      2. Asynchronous Module Definition (AMD)
      3. CommonJS
        1. exports versus module.exports
      4. Universal Module Definition (UMD)
    3. Implementing modules – the new way
      1. Importing/exporting modules
        1. Named versus default exports
      2. Naming named imports
      3. Wildcard imports
      4. Additional information on export
      5. Additional information on import
    4. Tree shaking
      1. How tree shaking is performed
    5. Using modules on the web
    6. Summary
  11. Implementing the Reflect API
    1. The Reflect object
      1. The Reflect.apply(function, this, args) method
      2. The Reflect.construct(constructor, args, prototype) method
      3. The Reflect.defineProperty(object, property, descriptor) method
        1. Understanding the data properties and accessor properties
      4. The Reflect.deleteProperty(object, property) method
      5. The Reflect.get(object, property, this) method
      6. The Reflect.set(object, property, value, this) method
      7. The Reflect.getOwnPropertyDescriptor(object, property) method
      8. The Reflect.getPrototypeOf(object) method
      9. The Reflect.setPrototypeOf(object, prototype) method
      10. The Reflect.has(object, property) method
      11. The Reflect.isExtensible(object) method
      12. The Reflect.preventExtensions(object) method
      13. The Reflect.ownKeys(object) method
    2. Summary
  12. Proxies
    1. Proxies in a nutshell
      1. Terminology for proxies
    2. Working with the Proxy API
      1. Proxy traps
        1. The get(target, property, receiver) method
          1. Rules for using get trap
        2. The set(target, property, value, receiver) method
          1. Rules for using set trap
        3. The has(target, property) method
          1. Rules for using has trap
        4. The isExtensible(target) method
          1. Rule for using isExtensible trap
        5. The getPrototypeOf(target) method
          1. Rules for using getPrototypeOf trap
        6. The setPrototypeOf(target, prototype) method
          1. Rule for using setPrototypeOf trap
        7. The preventExtensions(target) method
          1. Rule for using preventExtensions trap
        8. The getOwnPropertyDescriptor(target, property) method
          1. Rules for using getOwnPropertyDescriptor trap
        9. The defineProperty(target, property, descriptor) method
          1. Rule for using defineProperty
        10. The deleteProperty(target, property) method
          1. Rule for deleteProperty trap
        11. The ownKeys(target) method
          1. Rules for using ownKeys trap
        12. The apply(target, thisValue, arguments) method
        13. The construct(target, arguments) method
      2. The Proxy.revocable(target, handler) method
        1. Use case of revocable proxy
    3. The uses of proxies
    4. Summary
  13. Classes
    1. Understanding object-oriented JavaScript
      1. The JavaScript data types
      2. Creating objects
      3. Understanding the prototypal inheritance model
      4. The constructors of primitive data types
    2. Using classes
      1. Defining a class
        1. The class declaration
      2. The class expression
      3. The prototype methods
        1. Getters and setters
        2. The generator method
      4. Static methods
      5. Implementing inheritance in classes
      6. Computed method names
      7. The attributes of properties
      8. Classes are not hoisted!
      9. Overriding the result of the constructor method
      10. The Symbol.species static accessor property
      11. The new.target implicit parameter
    3. Using super in object literals
    4. Summary
  14. JavaScript on the Web
    1. HTML5 and the rise of modern JavaScript
      1. The HTML DOM
      2. What is the Document Object Model (DOM)?
      3. DOM methods/properties
        1. Using the getElementById method
        2. Using the getElementsByTagName method
        3. Using the getElementsByClassName method
        4. Using the querySelector method
        5. Using the querySelectorAll method
    2. Modern JavaScript browser APIs
      1. Page Visibility API - is the user still on the page?
      2. navigator.onLine API – the user's network status
      3. Clipboard API - programmatically manipulating the clipboard
      4. The Canvas API - the web's drawing board
      5. The Fetch API - promise-based HTTP requests
        1. Fetch API customization
      6. Accessing and modifying history with the history API
        1. Handling window.onpopstate events
        2. Modifying history - the history.go(distance) method
        3. Jumping ahead - the history.forward() method
        4. Going back - the history.back() method
        5. Pushing on the history - history.pushState()
        6. Pushing on the history stack - history.replaceState()
    3. Summary
  15. Storage APIs in JavaScript
    1. HyperText Transfer Protocol (HTTP)
      1. What is a TLS/SSL handshake?
      2. Mimicking an HTTP state
    2. Storing data with cookies
      1. Setting cookies
        1. The document.cookie is a strange object
      2. Deleting cookies
      3. Getting a cookie value
    3. Working with localStorage
      1. Creating a local storage entry
      2. Getting a stored item
      3. Removing a stored item
      4. Clearing all the items
      5. localStorage.getItem('key') versus localStorage.key versus localStorage['key']
    4. Working with SessionStorage
      1. Creating a session storage entry
      2. Getting a stored item
      3. Removing a stored item
      4. Clearing all items
    5. Handling storage changes across multiple tabs
    6. Cookies versus local storage
    7. The indexedDB - storing large data
      1. Opening an indexedDB database
      2. Handling the upgradeneeded event
      3. Adding data to object stores
      4. Reading data from object stores
      5. Deleting data from object stores
    8. Summary
  16. Web and Service Workers
    1. An introduction to the concept of threads
      1. What makes something thread-safe?
        1. What, exactly, is a deadlock?
        2. What, exactly, is a race condition?
    2. Introduction to web workers
    3. Checking if worker support is available
    4. Working with dedicated web workers
      1. Setting up a dedicated worker
      2. Working with dedicated workers
        1. Listening for messages on the main script
        2. Listening for messages on the worker script
        3. Sending messages from the main script
        4. Sending messages from the worker script
      3. Error handling in workers
      4. Terminating workers
        1. Terminating from the worker script
        2. Terminating from the main script
      5. Transferring (not copying) data through postMessage
    5. Working with shared workers
      1. Setting up a shared worker
      2. Working with shared workers
        1. Listening for messages on the main script
        2. Listening for messages on the worker script
        3. Sending messages from parent scripts
        4. Sending messages from the worker script
      3. Error handling
      4. Terminating a shared worker connection
        1. Terminating a single parent-worker connection
        2. Terminating a shared worker completely
    6. Introduction to inline web workers
    7. Same origin policy
    8. Working with service workers
      1. Prerequisites for service workers
      2. Checking for browser support
      3. The service worker life cycle
      4. Registering a service worker
      5. Installing service workers
      6. Fetching with service workers
    9. Summary
  17. Shared Memory and Atomics
    1. Basics of memory
      1. Abstraction of memory management
      2. Garbage collection
      3. Manually managing memory
    2. What is shared memory?
      1. Introduction to SharedArrayBuffer
    3. Understanding parallel programming
      1. Parallel versus concurrent programming
      2. Myth-busting--Parallel computation is always faster
      3. Let's count one billion!
      4. The race condition
    4. What are atomics?
      1. Information about lock and mutex
      2. Atomics in JavaScript
        1. Using the Atomics.load(typedArray, index) method
        2. Using the Atomics.add(typedArray, index, value) method
        3. Using the Atomics.sub(typedArray, index, value) method
        4. Using the Atomics.and(typedArray, index, value) method
          1. How bitwise AND works
        5. Using the Atomics.or(typedArray, index, value) method
          1. How bitwise OR works
        6. Using the Atomics.xor(typedArray, index, value) method
          1. How bitwise XOR works
    5. Fixing one billion count with atomics
      1. The optimized fix
    6. A peek into Spectre
    7. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learn ECMAScript - Second Edition
  • Author(s): Mehul Mohan, Narayan Prusty
  • Release date: February 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788620062