O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning ECMAScript 6

Book Description

Learn all the new ES6 features and be amongst the most prominent JavaScript developers who can write efficient JS programs as per the latest standards!

About This Book

  • Learn a powerful approach to writing object-oriented JavaScript code using ES6
  • Create and use ES6 modules to learn to write smart, modularized JavaScript code
  • The book will take you step-by-step through a wide array of examples, giving you tips on how to make the best use of the latest ES6 features

Who This Book Is For

If you are a JavaScript developer with basic development, and now want to learn about the latest features in order to develop better client-side programs with JavaScript, then this book is for you.

What You Will Learn

  • Explore the usage of new syntaxes introduced by ES6
  • Use the new prototype-based features introduced by ES6
  • Execute ES6 in an old non-supported ES6 environment
  • Write asynchronous code using promise to facilitate the writing of better asynchronous code that is easier to read and maintain
  • Create and understand the usage of iterators, iterables, and generators
  • Get to know object-oriented programming and create objects using classes
  • Build proxies using the ES6 proxy API and understand its uses
  • Create JavaScript libraries using ES6 modules

In Detail

ECMAScript 6 is the new edition to the ECMAScript language, whose specifications are inherited by JavaScript. ES6 gives a vast makeover to JavaScript by adding new syntaxes and APIs to write complex applications and libraries that are easier to debug and maintain. ES6 aims to keep JavaScript accessible for casual developers, even after adding so many new features.

This book will provide step-by-step instructions on how to apply ES6 features instead of old JavaScript hacks and methods.

The book will start by introducing all the built-in objects of ES6 and how to create ES6 custom Iterators. Next, it will teach you how to write asynchronous code in a synchronous style using ES6.

Moving on, the book will teach how to use Reflect API to inspect and manipulate object properties. Next, it teaches how to create proxies, and use it to intercept and customize operations performed on objects.

Finally, it explains old modular programming techniques such as IIFE, CommonJS, AMD, and UMD and also compares it with ES6 modules and how ES6 modules can increase the performance of websites when used.

Style and approach

This book is a pragmatic guide that takes you through the ES6 APIs and other features in a conversational and easy-to-follow style. Each topic is explained with examples and use cases.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Learning ECMAScript 6
    1. Table of Contents
    2. Learning ECMAScript 6
    3. Credits
    4. Foreword
    5. About the Author
    6. About the Reviewers
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. Preface
      1. What this book covers
      2. What you need for this book
        1. Compatibility with ECMAScript 6
        2. Running ECMAScript 6 in incompatible engines
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. Playing with Syntax
      1. The let keyword
        1. Declaring function scoped variables
        2. Declaring block scoped variables
        3. Re-declaring variables
      2. The const keyword
        1. The scope of constant variables
        2. Referencing the objects using constant variables
      3. Default parameter values
      4. The spread operator
        1. Other usages 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
      5. The rest parameter
      6. The destructuring assignment
        1. The array destructuring assignment
          1. Ignoring values
          2. Using the rest operator in the array destructuring assignment
          3. Default values for variables
          4. Nested array destructuring
          5. Using the destructuring assignment as a parameter
        2. The object destructuring assignment
          1. Default values for the variables
          2. Destructuring computed property names
          3. Destructuring nested objects
          4. Using the object destructuring assignment as a parameter
      7. The arrow functions
        1. The value of "this" in an arrow function
        2. Other differences between the arrow and traditional functions
      8. The enhanced object literals
        1. Defining properties
        2. Defining methods
        3. The computed property names
      9. Summary
    10. 2. 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. The Number.isFinite(number) method
        6. The Number.isSafeInteger(number) method
        7. The Number.EPSILON property
      2. Doing Math
        1. Trigonometry related operations
        2. Arithmetic related operations
        3. Miscellaneous 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
      3. Working with strings
        1. Escaping larger code points
        2. The codePointAt(index) method
        3. The String.fromCodePoint(number1, …, number 2) method
        4. The repeat(count) method
        5. The includes(string, index) method
        6. The startsWith(string, index) method
        7. The endsWith(string, index) function
        8. Normalization
          1. A case study
        9. Template strings
          1. Expressions
          2. Multiline strings
          3. Raw strings
      4. Arrays
        1. The Array.from(iterable, mapFunc, this) method
        2. The Array.of(values…) method
        3. The fill(value, startIndex, endIndex) method
        4. The find(testingFunc, this) method
        5. The findIndex(testingFunc, this) method
        6. The copyWithin(targetIndex, startIndex, endIndex) function
        7. The entries(), keys(), and values() method
      5. Collections
        1. Array buffers
        2. Typed arrays
        3. Set
        4. WeakSet
        5. Map
        6. WeakMap
      6. Object
        1. The __proto__ property
          1. The Object.is(value1, value2) method
          2. The Object.setPrototypeOf(object, prototype) method
          3. The Object.assign(targetObj, sourceObjs…) method
      7. Summary
    11. 3. Using Iterators
      1. The ES6 symbols
        1. The "typeof" operator
        2. The "new" operator
        3. Using symbols as property keys
        4. The Object.getOwnPropertySymbols() method
        5. The Symbol.for(string) method
        6. The well-known symbols
      2. The iteration protocols
        1. The iterator protocol
        2. The iterable protocol
      3. Generators
        1. The return(value) method
        2. The throw(exception) method
        3. The "yield*" keyword
      4. The "for…of" loop
      5. The tail call optimization
        1. Converting the non-tail calls into the tail calls
      6. Summary
    12. 4. Asynchronous Programming
      1. The JavaScript execution model
      2. Writing asynchronous code
        1. The asynchronous code involving events
        2. The asynchronous code involving callbacks
      3. Promises to the rescue
        1. The Promise constructor
        2. The fulfillment value
        3. The then(onFulfilled, onRejected) method
        4. The catch(onRejected) method
        5. The Promise.resolve(value) method
        6. The Promise.reject(value) method
        7. The Promise.all(iterable) method
        8. The Promise.race(iterable) method
      4. The JavaScript APIs based on Promises
        1. The Battery Status API
        2. The Web Cryptography API
      5. Summary
    13. 5. 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.enumerate(object) method
        6. The Reflect.get(object, property, this) method
        7. The Reflect.set(object, property, value, this) method
        8. The Reflect.getOwnPropertyDescriptor(object, property) method
        9. The Reflect.getPrototypeOf(object) method
        10. The Reflect.setPrototypeOf(object, prototype) method
        11. The Reflect.has(object, property) method
        12. The Reflect.isExtensible(object) method
        13. The Reflect.preventExtensions(object) method
        14. The Reflect.ownKeys(object) method
      2. Summary
    14. 6. Using Proxies
      1. Proxies in a nutshell
        1. Terminology
      2. The Proxy API
        1. Traps
          1. The get(target, property, receiver) method
            1. Rules
          2. The set(target, property, value, receiver) method
            1. Rules
          3. The has(target, property) method
            1. Rules
          4. The isExtensible(target) method
            1. Rules
          5. The getPrototypeOf(target) method
            1. Rules
          6. The setPrototypeOf(target, prototype) method
            1. Rules
          7. The preventExtensions(target) method
            1. Rules
          8. The getOwnPropertyDescriptor(target, property) method
            1. Rules
          9. The defineProperty(target, property, descriptor) method
            1. Rules
          10. The deleteProperty(target, property) method
            1. Rules
          11. The enumerate(target) method
            1. Rules
          12. The ownKeys(target) method
            1. Rules
          13. The apply(target, thisValue, arguments) method
          14. The construct(target, arguments) method
        2. The Proxy.revocable(target, handler) method
          1. Use Case
      3. The uses of proxies
      4. Summary
    15. 7. Walking You Through Classes
      1. Understanding the Object-oriented JavaScript
        1. The JavaScript data types
        2. Creating objects
        3. Understanding inheritance
        4. The constructors of primitive data types
      2. Using classes
        1. Defining a class
          1. The class declaration
          2. The class expression
        2. The prototype methods
          1. The get and set methods
          2. The generator method
        3. The static methods
        4. Implementing inheritance in classes
        5. The computed method names
        6. The attributes of properties
        7. Classes are not hoisted!
        8. Overriding the result of the constructor method
        9. The "Symbol.species" static accessor property
        10. The "new.target" implicit parameter
      3. Using "super" in the object literals
      4. Summary
    16. 8. Modular Programming
      1. The JavaScript modules in a nutshell
      2. Implementing modules – the old way
        1. The Immediately-Invoked Function Expression
        2. Asynchronous Module Definition
        3. CommonJS
        4. Universal Module Definition
      3. Implementing modules – the new way
        1. Creating the ES6 modules
        2. Importing the ES6 modules
        3. The module loader
        4. Using modules in browsers
        5. Using modules in the eval() function
        6. The default exports vs. the named exports
        7. Diving into an example
      4. Summary
    17. Index