Exploring ES6
Homepage
Please support this book: buy it (PDF, EPUB, MOBI) or donate
(Ad, please don’t block.)

Table of contents

    1. Short TOC
    2. What you need to know about this book
      1. Audience: JavaScript programmers
      2. Why should I read this book?
      3. How to read this book
      4. Sources of this book
      5. Glossary
        1. Strict mode versus sloppy mode
        2. Protocol
        3. Receiver (of a method call)
        4. Signature of a function (or a method)
        5. Internal properties
        6. Bindings and environments
      6. Conventions
        1. Documenting classes
        2. Capitalization
      7. Demo code on GitHub
      8. Sidebars
      9. Footnotes
    3. Foreword
    4. Preface
    5. Acknowledgements
    6. About the author
  1. I Background
    1. 1. About ECMAScript 6 (ES6)
      1. 1.1 TC39 (Ecma Technical Committee 39)
      2. 1.2 How ECMAScript 6 was designed
        1. 1.2.1 The design process after ES6
      3. 1.3 JavaScript versus ECMAScript
      4. 1.4 Upgrading to ES6
      5. 1.5 Goals for ES6
        1. 1.5.1 Goal: Be a better language
        2. 1.5.2 Goal: Improve interoperation
        3. 1.5.3 Goal: Versioning
      6. 1.6 Categories of ES6 features
      7. 1.7 A brief history of ECMAScript
        1. 1.7.1 The early years: ECMAScript 1–3
        2. 1.7.2 ECMAScript 4 (abandoned in July 2008)
        3. 1.7.3 ECMAScript Harmony
    2. 2. FAQ: ECMAScript 6
      1. 2.1 How can I use ES6 today?
      2. 2.2 Isn’t ECMAScript 6 now called ECMAScript 2015?
      3. 2.3 How do I migrate my ECMAScript 5 code to ECMAScript 6?
      4. 2.4 Does it still make sense to learn ECMAScript 5?
      5. 2.5 Is ES6 bloated?
      6. 2.6 Isn’t the ES6 specification very big?
      7. 2.7 Does ES6 have array comprehensions?
      8. 2.8 Is ES6 statically typed?
      9. 2.9 Where can I find more ES6 resources?
    3. 3. One JavaScript: avoiding versioning in ECMAScript 6
      1. 3.1 Versioning
        1. 3.1.1 Evolution without versioning
      2. 3.2 Strict mode and ECMAScript 6
        1. 3.2.1 Supporting sloppy (non-strict) mode
        2. 3.2.2 let declarations in sloppy mode
        3. 3.2.3 Block-level function declarations in sloppy mode
        4. 3.2.4 Other keywords
        5. 3.2.5 Implicit strict mode
        6. 3.2.6 Things that can’t be fixed
      3. 3.3 Breaking changes in ES6
      4. 3.4 Conclusion
      5. 3.5 Further reading
    4. 4. First steps with ECMAScript 6
      1. 4.1 From var to let/const
      2. 4.2 From IIFEs to blocks
      3. 4.3 From concatenating strings to template literals
        1. 4.3.1 String interpolation
        2. 4.3.2 Multi-line strings
      4. 4.4 From function expressions to arrow functions
      5. 4.5 Handling multiple return values
        1. 4.5.1 Multiple return values via arrays
        2. 4.5.2 Multiple return values via objects
      6. 4.6 From for to forEach() to for-of
      7. 4.7 Handling parameter default values
      8. 4.8 Handling named parameters
        1. 4.8.1 Making the parameter optional
      9. 4.9 From arguments to rest parameters
      10. 4.10 From apply() to the spread operator (...)
        1. 4.10.1 Math.max()
        2. 4.10.2 Array.prototype.push()
      11. 4.11 From concat() to the spread operator (...)
      12. 4.12 From function expressions in object literals to method definitions
      13. 4.13 From constructors to classes
        1. 4.13.1 Base classes
        2. 4.13.2 Derived classes
      14. 4.14 From custom error constructors to subclasses of Error
      15. 4.15 From objects to Maps
      16. 4.16 New string methods
      17. 4.17 New Array methods
        1. 4.17.1 From Array.prototype.indexOf to Array.prototype.findIndex
        2. 4.17.2 From Array.prototype.slice() to Array.from()
        3. 4.17.3 From apply() to Array.prototype.fill()
      18. 4.18 From CommonJS modules to ES6 modules
        1. 4.18.1 Multiple exports
        2. 4.18.2 Single exports
      19. 4.19 What to do next
  2. II Data
    1. 5. New number and Math features
      1. 5.1 Overview
        1. 5.1.1 New integer literals
        2. 5.1.2 New Number properties
        3. 5.1.3 New Math methods
      2. 5.2 New integer literals
        1. 5.2.1 Use case for octal literals: Unix-style file permissions
        2. 5.2.2 parseInt() and the new integer literals
      3. 5.3 New static Number properties
        1. 5.3.1 Previously global functions
        2. 5.3.2 Number.EPSILON
        3. 5.3.3 Number.isInteger(number)
        4. 5.3.4 Safe integers
      4. 5.4 Math
        1. 5.4.1 Various numerical functionality
        2. 5.4.2 Using 0 instead of 1 with exponentiation and logarithm
        3. 5.4.3 Logarithms to base 2 and 10
        4. 5.4.4 Support for compiling to JavaScript
        5. 5.4.5 Bitwise operations
        6. 5.4.6 Trigonometric methods
      5. 5.5 FAQ: numbers
        1. 5.5.1 How can I use integers beyond JavaScript’s 53 bit range?
    2. 6. New string features
      1. 6.1 Overview
      2. 6.2 Unicode code point escapes
      3. 6.3 String interpolation, multi-line string literals and raw string literals
      4. 6.4 Iterating over strings
        1. 6.4.1 Iteration honors Unicode code points
        2. 6.4.2 Counting code points
        3. 6.4.3 Reversing strings with non-BMP code points
      5. 6.5 Numeric values of code points
      6. 6.6 Checking for inclusion
      7. 6.7 Repeating strings
      8. 6.8 String methods that delegate regular expression work to their parameters
      9. 6.9 Cheat sheet: the new string methods
    3. 7. Symbols
      1. 7.1 Overview
        1. 7.1.1 Use case 1: unique property keys
        2. 7.1.2 Use case 2: constants representing concepts
        3. 7.1.3 Pitfall: you can’t coerce symbols to strings
        4. 7.1.4 Which operations related to property keys are aware of symbols?
      2. 7.2 A new primitive type
        1. 7.2.1 Symbols as property keys
        2. 7.2.2 Enumerating own property keys
      3. 7.3 Using symbols to represent concepts
      4. 7.4 Symbols as keys of properties
        1. 7.4.1 Symbols as keys of non-public properties
        2. 7.4.2 Symbols as keys of meta-level properties
        3. 7.4.3 Examples of name clashes in JavaScript’s standard library
      5. 7.5 Converting symbols to other primitive types
        1. 7.5.1 Pitfall: coercion to string
        2. 7.5.2 Making sense of the coercion rules
        3. 7.5.3 Explicit and implicit conversion in the spec
      6. 7.6 JSON and symbols
        1. 7.6.1 Generating JSON via JSON.stringify()
        2. 7.6.2 Parsing JSON via JSON.parse()
      7. 7.7 Wrapper objects for symbols
        1. 7.7.1 Accessing properties via [ ] and wrapped keys
      8. 7.8 Crossing realms with symbols
      9. 7.9 FAQ: symbols
        1. 7.9.1 Can I use symbols to define private properties?
        2. 7.9.2 Are symbols primitives or objects?
        3. 7.9.3 Do we really need symbols? Aren’t strings enough?
        4. 7.9.4 Are JavaScript’s symbols like Ruby’s symbols?
      10. 7.10 The spelling of well-known symbols: why Symbol.iterator and not Symbol.ITERATOR (etc.)?
      11. 7.11 The symbol API
        1. 7.11.1 The function Symbol
        2. 7.11.2 Methods of symbols
        3. 7.11.3 Converting symbols to other values
        4. 7.11.4 Well-known symbols
        5. 7.11.5 Global symbol registry
    4. 8. Template literals
      1. 8.1 Overview
      2. 8.2 Introduction
        1. 8.2.1 Template literals
        2. 8.2.2 Line terminators in template literals are always LF (\n)
        3. 8.2.3 Tagged template literals
      3. 8.3 Examples of using tagged template literals
        1. 8.3.1 Raw strings
        2. 8.3.2 Shell commands
        3. 8.3.3 Byte strings
        4. 8.3.4 HTTP requests
        5. 8.3.5 More powerful regular expressions
        6. 8.3.6 Query languages
        7. 8.3.7 React JSX via tagged templates
        8. 8.3.8 Facebook GraphQL
        9. 8.3.9 Text localization (L10N)
        10. 8.3.10 Text templating via untagged template literals
        11. 8.3.11 A tag function for HTML templating
      4. 8.4 Implementing tag functions
        1. 8.4.1 Escaping in tagged template literals: cooked versus raw
        2. 8.4.2 Example: implementing a tag function for HTML templating
        3. 8.4.3 Example: assembling regular expressions
      5. 8.5 FAQ: template literals and tagged template literals
        1. 8.5.1 Where do template literals and tagged template literals come from?
        2. 8.5.2 What is the difference between macros and tagged template literals?
        3. 8.5.3 Can I load a template literal from an external source?
        4. 8.5.4 Why are backticks the delimiters for template literals?
        5. 8.5.5 Weren’t template literals once called template strings?
    5. 9. Variables and scoping
      1. 9.1 Overview
        1. 9.1.1 let
        2. 9.1.2 const
        3. 9.1.3 Ways of declaring variables
      2. 9.2 Block scoping via let and const
      3. 9.3 const creates immutable variables
        1. 9.3.1 Pitfall: const does not make the value immutable
        2. 9.3.2 const in loop bodies
      4. 9.4 The temporal dead zone
        1. 9.4.1 The life cycle of var-declared variables
        2. 9.4.2 The life cycle of let-declared variables
        3. 9.4.3 Examples
        4. 9.4.4 typeof throws a ReferenceError for a variable in the TDZ
        5. 9.4.5 Why is there a temporal dead zone?
        6. 9.4.6 Further reading
      5. 9.5 let and const in loop heads
        1. 9.5.1 for loop
        2. 9.5.2 for-of loop and for-in loop
        3. 9.5.3 Why are per-iteration bindings useful?
      6. 9.6 Parameters
        1. 9.6.1 Parameters versus local variables
        2. 9.6.2 Parameter default values and the temporal dead zone
        3. 9.6.3 Parameter default values don’t see the scope of the body
      7. 9.7 The global object
      8. 9.8 Function declarations and class declarations
      9. 9.9 Coding style: const versus let versus var
        1. 9.9.1 An alternative approach
    6. 10. Destructuring
      1. 10.1 Overview
        1. 10.1.1 Object destructuring
        2. 10.1.2 Array destructuring
        3. 10.1.3 Where can destructuring be used?
      2. 10.2 Background: Constructing data versus extracting data
      3. 10.3 Patterns
        1. 10.3.1 Pick what you need
      4. 10.4 How do patterns access the innards of values?
        1. 10.4.1 Object patterns coerce values to objects
        2. 10.4.2 Array patterns work with iterables
      5. 10.5 If a part has no match
        1. 10.5.1 Default values
      6. 10.6 More object destructuring features
        1. 10.6.1 Property value shorthands
        2. 10.6.2 Computed property keys
      7. 10.7 More Array destructuring features
        1. 10.7.1 Elision
        2. 10.7.2 Rest operator (...)
      8. 10.8 You can assign to more than just variables
      9. 10.9 Pitfalls of destructuring
        1. 10.9.1 Don’t start a statement with a curly brace
        2. 10.9.2 You can’t mix declaring and assigning to existing variables
      10. 10.10 Examples of destructuring
        1. 10.10.1 Destructuring returned Arrays
        2. 10.10.2 Destructuring returned objects
        3. 10.10.3 Array-destructuring iterable values
        4. 10.10.4 Multiple return values
      11. 10.11 The destructuring algorithm
        1. 10.11.1 The algorithm
        2. 10.11.2 Applying the algorithm
        3. 10.11.3 Conclusion
    7. 11. Parameter handling
      1. 11.1 Overview
        1. 11.1.1 Spread operator (...)
      2. 11.2 Parameter handling as destructuring
      3. 11.3 Parameter default values
        1. 11.3.1 Why does undefined trigger default values?
        2. 11.3.2 Referring to other parameters in default values
        3. 11.3.3 Referring to “inner” variables in default values
      4. 11.4 Rest parameters
        1. 11.4.1 No more arguments!
      5. 11.5 Simulating named parameters
        1. 11.5.1 Named Parameters as Descriptions
        2. 11.5.2 Optional Named Parameters
        3. 11.5.3 Simulating Named Parameters in JavaScript
      6. 11.6 Examples of destructuring in parameter handling
        1. 11.6.1 forEach() and destructuring
        2. 11.6.2 Transforming Maps
        3. 11.6.3 Handling an Array returned via a Promise
      7. 11.7 Coding style tips
        1. 11.7.1 Optional parameters
        2. 11.7.2 Required parameters
        3. 11.7.3 Enforcing a maximum arity
      8. 11.8 The spread operator (...)
        1. 11.8.1 Spreading into function and method calls
        2. 11.8.2 Spreading into constructors
        3. 11.8.3 Spreading into Arrays
  3. III Modularity
    1. 12. Callable entities in ECMAScript 6
      1. 12.1 Overview
      2. 12.2 Ways of calling in ES6
        1. 12.2.1 Calls that can be made anywhere
        2. 12.2.2 Calls via super are restricted to specific locations
        3. 12.2.3 Non-method functions versus methods
      3. 12.3 Recommendations for using callable entities
        1. 12.3.1 Prefer arrow functions as callbacks
        2. 12.3.2 Prefer function declarations as stand-alone functions
        3. 12.3.3 Prefer method definitions for methods
        4. 12.3.4 Methods versus callbacks
        5. 12.3.5 Avoid IIFEs in ES6
        6. 12.3.6 Use classes as constructors
      4. 12.4 ES6 callable entities in detail
        1. 12.4.1 Cheat sheet: callable entities
        2. 12.4.2 Traditional functions
        3. 12.4.3 Generator functions
        4. 12.4.4 Method definitions
        5. 12.4.5 Generator method definitions
        6. 12.4.6 Arrow functions
        7. 12.4.7 Classes
      5. 12.5 Dispatched and direct method calls in ES5 and ES6
        1. 12.5.1 Background: prototype chains
        2. 12.5.2 Dispatched method calls
        3. 12.5.3 Direct method calls
        4. 12.5.4 Use cases for direct method calls
        5. 12.5.5 Abbreviations for Object.prototype and Array.prototype
      6. 12.6 The name property of functions
        1. 12.6.1 Constructs that provide names for functions
        2. 12.6.2 Caveats
        3. 12.6.3 Changing the names of functions
        4. 12.6.4 The function property name in the spec
      7. 12.7 FAQ: callable entities
        1. 12.7.1 Why are there “fat” arrow functions (=>) in ES6, but no “thin” arrow functions (->)?
        2. 12.7.2 How do I determine whether a function was invoked via new?
    2. 13. Arrow functions
      1. 13.1 Overview
      2. 13.2 Traditional functions are bad non-method functions, due to this
        1. 13.2.1 Solution 1: that = this
        2. 13.2.2 Solution 2: specifying a value for this
        3. 13.2.3 Solution 3: bind(this)
        4. 13.2.4 ECMAScript 6 solution: arrow functions
      3. 13.3 Arrow function syntax
        1. 13.3.1 Omitting parentheses around single parameters
      4. 13.4 Lexical variables
        1. 13.4.1 Sources of variable values: static versus dynamic
        2. 13.4.2 Variables that are lexical in arrow functions
      5. 13.5 Syntax pitfalls
        1. 13.5.1 Arrow functions bind very loosely
        2. 13.5.2 No line break after arrow function parameters
        3. 13.5.3 You can’t use statements as expression bodies
        4. 13.5.4 Returning object literals
      6. 13.6 Immediately-invoked arrow functions
      7. 13.7 Arrow functions vs. bind()
        1. 13.7.1 Extracting methods
        2. 13.7.2 this via parameters
        3. 13.7.3 Partial evaluation
      8. 13.8 Arrow functions versus normal functions
    3. 14. New OOP features besides classes
      1. 14.1 Overview
        1. 14.1.1 New object literal features
        2. 14.1.2 New methods in Object
      2. 14.2 New features of object literals
        1. 14.2.1 Method definitions
        2. 14.2.2 Property value shorthands
        3. 14.2.3 Computed property keys
      3. 14.3 New methods of Object
        1. 14.3.1 Object.assign(target, source_1, source_2, ···)
        2. 14.3.2 Object.getOwnPropertySymbols(obj)
        3. 14.3.3 Object.is(value1, value2)
        4. 14.3.4 Object.setPrototypeOf(obj, proto)
      4. 14.4 Traversing properties in ES6
        1. 14.4.1 Five operations that traverse properties
        2. 14.4.2 Traversal order of properties
      5. 14.5 Assigning versus defining properties
        1. 14.5.1 Overriding inherited read-only properties
      6. 14.6 __proto__ in ECMAScript 6
        1. 14.6.1 __proto__ prior to ECMAScript 6
        2. 14.6.2 The two kinds of __proto__ in ECMAScript 6
        3. 14.6.3 Avoiding the magic of __proto__
        4. 14.6.4 Detecting support for ES6-style __proto__
        5. 14.6.5 __proto__ is pronounced “dunder proto”
        6. 14.6.6 Recommendations for __proto__
      7. 14.7 Enumerability in ECMAScript 6
        1. 14.7.1 Property attributes
        2. 14.7.2 Constructs affected by enumerability
        3. 14.7.3 Use cases for enumerability
        4. 14.7.4 Naming inconsistencies
        5. 14.7.5 Looking ahead
      8. 14.8 Customizing basic language operations via well-known symbols
        1. 14.8.1 Property key Symbol.hasInstance (method)
        2. 14.8.2 Property key Symbol.toPrimitive (method)
        3. 14.8.3 Property key Symbol.toStringTag (string)
        4. 14.8.4 Property key Symbol.unscopables (Object)
      9. 14.9 FAQ: object literals
        1. 14.9.1 Can I use super in object literals?
    4. 15. Classes
      1. 15.1 Overview
      2. 15.2 The essentials
        1. 15.2.1 Base classes
        2. 15.2.2 Inside the body of a class definition
        3. 15.2.3 Subclassing
      3. 15.3 Private data for classes
        1. 15.3.1 Private data via constructor environments
        2. 15.3.2 Private data via a naming convention
        3. 15.3.3 Private data via WeakMaps
        4. 15.3.4 Private data via symbols
        5. 15.3.5 Further reading
      4. 15.4 Simple mixins
      5. 15.5 The details of classes
        1. 15.5.1 Various checks
        2. 15.5.2 Attributes of properties
        3. 15.5.3 Classes have inner names
      6. 15.6 The details of subclassing
        1. 15.6.1 Prototype chains
        2. 15.6.2 Allocating and initializing instances
        3. 15.6.3 Why can’t you subclass built-in constructors in ES5?
        4. 15.6.4 Referring to superproperties in methods
      7. 15.7 The species pattern
        1. 15.7.1 Helper methods for examples
        2. 15.7.2 The standard species pattern
        3. 15.7.3 The species pattern for Arrays
        4. 15.7.4 The species pattern in static methods
        5. 15.7.5 Overriding the default species in subclasses
      8. 15.8 The pros and cons of classes
        1. 15.8.1 Complaint: ES6 classes obscure the true nature of JavaScript inheritance
        2. 15.8.2 Complaint: Classes provide only single inheritance
        3. 15.8.3 Complaint: Classes lock you in, due to mandatory new
      9. 15.9 FAQ: classes
        1. 15.9.1 Why can’t classes be function-called?
        2. 15.9.2 How do I instantiate a class, given an Array of arguments?
      10. 15.10 What is next for classes?
      11. 15.11 Further reading
    5. 16. Modules
      1. 16.1 Overview
        1. 16.1.1 Multiple named exports
        2. 16.1.2 Single default export
        3. 16.1.3 Browsers: scripts versus modules
      2. 16.2 Modules in JavaScript
        1. 16.2.1 ECMAScript 5 module systems
        2. 16.2.2 ECMAScript 6 modules
      3. 16.3 The basics of ES6 modules
        1. 16.3.1 Named exports (several per module)
        2. 16.3.2 Default exports (one per module)
        3. 16.3.3 Imports and exports must be at the top level
        4. 16.3.4 Imports are hoisted
        5. 16.3.5 Imports are read-only views on exports
        6. 16.3.6 Support for cyclic dependencies
      4. 16.4 Importing and exporting in detail
        1. 16.4.1 Importing styles
        2. 16.4.2 Named exporting styles: inline versus clause
        3. 16.4.3 Re-exporting
        4. 16.4.4 All exporting styles
        5. 16.4.5 Having both named exports and a default export in a module
      5. 16.5 The ECMAScript 6 module loader API
        1. 16.5.1 Loaders
        2. 16.5.2 Loader method: importing modules
        3. 16.5.3 More loader methods
        4. 16.5.4 Configuring module loading
      6. 16.6 Using ES6 modules in browsers
        1. 16.6.1 Browsers: asynchronous modules versus synchronous scripts
      7. 16.7 Details: imports as views on exports
        1. 16.7.1 In CommonJS, imports are copies of exported values
        2. 16.7.2 In ES6, imports are live read-only views on exported values
        3. 16.7.3 Implementing views
        4. 16.7.4 Imports as views in the spec
      8. 16.8 Design goals for ES6 modules
        1. 16.8.1 Default exports are favored
        2. 16.8.2 Static module structure
        3. 16.8.3 Support for both synchronous and asynchronous loading
        4. 16.8.4 Support for cyclic dependencies between modules
      9. 16.9 FAQ: modules
        1. 16.9.1 Can I use a variable to specify from which module I want to import?
        2. 16.9.2 Can I import a module conditionally or on demand?
        3. 16.9.3 Can I use variables in an import statement?
        4. 16.9.4 Can I use destructuring in an import statement?
        5. 16.9.5 Are named exports necessary? Why not default-export objects?
        6. 16.9.6 Can I eval() the code of module?
      10. 16.10 Advantages of ECMAScript 6 modules
      11. 16.11 Further reading
  4. IV Collections
    1. 17. The for-of loop
      1. 17.1 Overview
      2. 17.2 Introducing the for-of loop
      3. 17.3 Pitfall: for-of only works with iterable values
      4. 17.4 Iteration variables: const declarations versus var declarations
      5. 17.5 Iterating with existing variables, object properties and Array elements
      6. 17.6 Iterating with a destructuring pattern
    2. 18. New Array features
      1. 18.1 Overview
      2. 18.2 New static Array methods
        1. 18.2.1 Array.from(arrayLike, mapFunc?, thisArg?)
        2. 18.2.2 Array.of(...items)
      3. 18.3 New Array.prototype methods
        1. 18.3.1 Iterating over Arrays
        2. 18.3.2 Searching for Array elements
        3. 18.3.3 Array.prototype.copyWithin()
        4. 18.3.4 Array.prototype.fill()
      4. 18.4 ES6 and holes in Arrays
        1. 18.4.1 ECMAScript 6 treats holes like undefined elements
        2. 18.4.2 Array operations and holes
        3. 18.4.3 Creating Arrays filled with values
        4. 18.4.4 Removing holes from Arrays
      5. 18.5 Configuring which objects are spread by concat() (Symbol.isConcatSpreadable)
        1. 18.5.1 Default for Arrays: spreading
        2. 18.5.2 Default for non-Arrays: no spreading
        3. 18.5.3 Detecting Arrays
        4. 18.5.4 Symbol.isConcatSpreadable in the standard library
      6. 18.6 The numeric range of Array indices
    3. 19. Maps and Sets
      1. 19.1 Overview
        1. 19.1.1 Maps
        2. 19.1.2 Sets
        3. 19.1.3 WeakMaps
      2. 19.2 Map
        1. 19.2.1 Basic operations
        2. 19.2.2 Setting up a Map
        3. 19.2.3 Keys
        4. 19.2.4 Iterating over Maps
        5. 19.2.5 Looping over Map entries
        6. 19.2.6 Mapping and filtering Maps
        7. 19.2.7 Combining Maps
        8. 19.2.8 Arbitrary Maps as JSON via Arrays of pairs
        9. 19.2.9 String Maps as JSON via objects
        10. 19.2.10 Map API
      3. 19.3 WeakMap
        1. 19.3.1 WeakMap keys are objects
        2. 19.3.2 WeakMap keys are weakly held
        3. 19.3.3 You can’t get an overview of a WeakMap or clear it
        4. 19.3.4 Use cases for WeakMaps
        5. 19.3.5 WeakMap API
      4. 19.4 Set
        1. 19.4.1 Basic operations
        2. 19.4.2 Setting up a Set
        3. 19.4.3 Comparing Set elements
        4. 19.4.4 Iterating
        5. 19.4.5 Mapping and filtering
        6. 19.4.6 Union, intersection, difference
        7. 19.4.7 Set API
      5. 19.5 WeakSet
        1. 19.5.1 Use cases for WeakSets
        2. 19.5.2 WeakSet API
      6. 19.6 FAQ: Maps and Sets
        1. 19.6.1 Why do Maps and Sets have the property size and not length?
        2. 19.6.2 Why can’t I configure how Maps and Sets compare keys and values?
        3. 19.6.3 Is there a way to specify a default value when getting something out of a Map?
        4. 19.6.4 When should I use a Map, when an object?
        5. 19.6.5 When would I use an object as a key in a Map?
    4. 20. Typed Arrays
      1. 20.1 Overview
      2. 20.2 Introduction
        1. 20.2.1 Element types
        2. 20.2.2 Handling overflow and underflow
        3. 20.2.3 Endianness
        4. 20.2.4 Negative indices
      3. 20.3 ArrayBuffers
        1. 20.3.1 ArrayBuffer constructor
        2. 20.3.2 Static ArrayBuffer methods
        3. 20.3.3 ArrayBuffer.prototype properties
      4. 20.4 Typed Arrays
        1. 20.4.1 Typed Arrays versus normal Arrays
        2. 20.4.2 Typed Arrays are iterable
        3. 20.4.3 Converting Typed Arrays to and from normal Arrays
        4. 20.4.4 The Species pattern for Typed Arrays
        5. 20.4.5 The inheritance hierarchy of Typed Arrays
        6. 20.4.6 Static TypedArray methods
        7. 20.4.7 TypedArray.prototype properties
        8. 20.4.8 «ElementType»Array constructor
        9. 20.4.9 Static «ElementType»Array properties
        10. 20.4.10 «ElementType»Array.prototype properties
        11. 20.4.11 Concatenating Typed Arrays
      5. 20.5 DataViews
        1. 20.5.1 DataView constructor
        2. 20.5.2 DataView.prototype properties
      6. 20.6 Browser APIs that support Typed Arrays
        1. 20.6.1 File API
        2. 20.6.2 XMLHttpRequest
        3. 20.6.3 Fetch API
        4. 20.6.4 Canvas
        5. 20.6.5 WebSockets
        6. 20.6.6 Other APIs
      7. 20.7 Extended example: JPEG SOF0 decoder
        1. 20.7.1 The JPEG file format
        2. 20.7.2 The JavaScript code
      8. 20.8 Availability
    5. 21. Iterables and iterators
      1. 21.1 Overview
        1. 21.1.1 Iterable values
        2. 21.1.2 Constructs supporting iteration
      2. 21.2 Iterability
      3. 21.3 Iterable data sources
        1. 21.3.1 Arrays
        2. 21.3.2 Strings
        3. 21.3.3 Maps
        4. 21.3.4 Sets
        5. 21.3.5 arguments
        6. 21.3.6 DOM data structures
        7. 21.3.7 Iterable computed data
        8. 21.3.8 Plain objects are not iterable
      4. 21.4 Iterating language constructs
        1. 21.4.1 Destructuring via an Array pattern
        2. 21.4.2 The for-of loop
        3. 21.4.3 Array.from()
        4. 21.4.4 The spread operator (...)
        5. 21.4.5 Maps and Sets
        6. 21.4.6 Promises
        7. 21.4.7 yield*
      5. 21.5 Implementing iterables
        1. 21.5.1 Iterators that are iterable
        2. 21.5.2 Optional iterator methods: return() and throw()
      6. 21.6 More examples of iterables
        1. 21.6.1 Tool functions that return iterables
        2. 21.6.2 Combinators for iterables
        3. 21.6.3 Infinite iterables
      7. 21.7 FAQ: iterables and iterators
        1. 21.7.1 Isn’t the iteration protocol slow?
        2. 21.7.2 Can I reuse the same object several times?
        3. 21.7.3 Why doesn’t ECMAScript 6 have iterable combinators?
        4. 21.7.4 Aren’t iterables difficult to implement?
      8. 21.8 The ECMAScript 6 iteration protocol in depth
        1. 21.8.1 Iteration
        2. 21.8.2 Closing iterators
        3. 21.8.3 Checklist
    6. 22. Generators
      1. 22.1 Overview
        1. 22.1.1 Implementing iterables via generators
        2. 22.1.2 Blocking on asynchronous function calls
      2. 22.2 What are generators?
        1. 22.2.1 Kinds of generators
        2. 22.2.2 Roles played by generators
      3. 22.3 Generators as iterators (data production)
        1. 22.3.1 Ways of iterating over a generator
        2. 22.3.2 Returning from a generator
        3. 22.3.3 Throwing an exception from a generator
        4. 22.3.4 Example: iterating over properties
        5. 22.3.5 You can only yield in generators
        6. 22.3.6 Recursion via yield*
      4. 22.4 Generators as observers (data consumption)
        1. 22.4.1 Sending values via next()
        2. 22.4.2 yield binds loosely
        3. 22.4.3 return() and throw()
        4. 22.4.4 return() terminates the generator
        5. 22.4.5 throw() signals an error
        6. 22.4.6 Example: processing asynchronously pushed data
        7. 22.4.7 yield*: the full story
      5. 22.5 Generators as coroutines (cooperative multitasking)
        1. 22.5.1 The full generator interface
        2. 22.5.2 Cooperative multitasking
        3. 22.5.3 The limitations of cooperative multitasking via generators
      6. 22.6 Examples of generators
        1. 22.6.1 Implementing iterables via generators
        2. 22.6.2 Generators for lazy evaluation
        3. 22.6.3 Cooperative multi-tasking via generators
      7. 22.7 Inheritance within the iteration API (including generators)
        1. 22.7.1 IteratorPrototype
        2. 22.7.2 The value of this in generators
      8. 22.8 Style consideration: whitespace before and after the asterisk
        1. 22.8.1 Generator function declarations and expressions
        2. 22.8.2 Generator method definitions
        3. 22.8.3 Formatting recursive yield
        4. 22.8.4 Documenting generator functions and methods
      9. 22.9 FAQ: generators
        1. 22.9.1 Why use the keyword function* for generators and not generator?
        2. 22.9.2 Is yield a keyword?
      10. 22.10 Conclusion
      11. 22.11 Further reading
  5. V Standard library
    1. 23. New regular expression features
      1. 23.1 Overview
      2. 23.2 New flag /y (sticky)
        1. 23.2.1 RegExp.prototype.exec(str)
        2. 23.2.2 RegExp.prototype.test(str)
        3. 23.2.3 String.prototype.search(regex)
        4. 23.2.4 String.prototype.match(regex)
        5. 23.2.5 String.prototype.split(separator, limit)
        6. 23.2.6 String.prototype.replace(search, replacement)
        7. 23.2.7 Example: using sticky matching for tokenizing
        8. 23.2.8 Example: manually implementing sticky matching
      3. 23.3 New flag /u (unicode)
        1. 23.3.1 Consequence: lone surrogates in the regular expression only match lone surrogates
        2. 23.3.2 Consequence: you can put code points in character classes
        3. 23.3.3 Consequence: the dot operator (.) matches code points, not code units
        4. 23.3.4 Consequence: quantifiers apply to code points, not code units
      4. 23.4 New data property flags
      5. 23.5 RegExp() can be used as a copy constructor
        1. 23.5.1 Example: an iterable version of exec()
      6. 23.6 String methods that delegate to regular expression methods
    2. 24. Asynchronous programming (background)
      1. 24.1 The JavaScript call stack
      2. 24.2 The browser event loop
        1. 24.2.1 Timers
        2. 24.2.2 Displaying DOM changes
        3. 24.2.3 Run-to-completion semantics
        4. 24.2.4 Blocking the event loop
        5. 24.2.5 Avoiding blocking
      3. 24.3 Receiving results asynchronously
        1. 24.3.1 Asynchronous results via events
        2. 24.3.2 Asynchronous results via callbacks
        3. 24.3.3 Continuation-passing style
        4. 24.3.4 Composing code in CPS
        5. 24.3.5 Pros and cons of callbacks
      4. 24.4 Looking ahead
      5. 24.5 Further reading
    3. 25. Promises for asynchronous programming
      1. 25.1 Overview
        1. 25.1.1 Chaining then() calls
        2. 25.1.2 Executing asynchronous functions in parallel
        3. 25.1.3 Glossary: Promises
      2. 25.2 Introduction: Promises
      3. 25.3 A first example
      4. 25.4 Creating and using Promises
        1. 25.4.1 Producing a Promise
        2. 25.4.2 The states of Promises
        3. 25.4.3 Consuming a Promise
        4. 25.4.4 Promises are always asynchronous
      5. 25.5 Examples
        1. 25.5.1 Example: promisifying fs.readFile()
        2. 25.5.2 Example: promisifying XMLHttpRequest
        3. 25.5.3 Example: delaying an activity
        4. 25.5.4 Example: timing out a Promise
      6. 25.6 Other ways of creating Promises
        1. 25.6.1 Promise.resolve()
        2. 25.6.2 Promise.reject()
      7. 25.7 Chaining Promises
        1. 25.7.1 Resolving Q with a normal value
        2. 25.7.2 Resolving Q with a thenable
        3. 25.7.3 Resolving Q from onRejected
        4. 25.7.4 Rejecting Q by throwing an exception
        5. 25.7.5 Chaining and errors
      8. 25.8 Common Promise chaining mistakes
        1. 25.8.1 Mistake: losing the tail of a Promise chain
        2. 25.8.2 Mistake: nesting Promises
        3. 25.8.3 Mistake: creating Promises instead of chaining
        4. 25.8.4 Mistake: using then() for error handling
      9. 25.9 Tips for error handling
        1. 25.9.1 Operational errors vs. programmer errors
        2. 25.9.2 Handling exceptions in Promise-based functions
        3. 25.9.3 Further reading
      10. 25.10 Composing Promises
        1. 25.10.1 Manually forking and joining computations
        2. 25.10.2 Forking and joining computations via Promise.all()
        3. 25.10.3 map() via Promise.all()
        4. 25.10.4 Timing out via Promise.race()
      11. 25.11 Two useful additional Promise methods
        1. 25.11.1 done()
        2. 25.11.2 finally()
      12. 25.12 Node.js: using callback-based sync functions with Promises
      13. 25.13 ES6-compatible Promise libraries
      14. 25.14 Promises and generators
      15. 25.15 Next step: using Promises via generators
      16. 25.16 Promises in depth: a simple implementation
        1. 25.16.1 A stand-alone Promise
        2. 25.16.2 Chaining
        3. 25.16.3 Flattening
        4. 25.16.4 Promise states in more detail
        5. 25.16.5 Exceptions
        6. 25.16.6 Revealing constructor pattern
      17. 25.17 Advantages and limitations of Promises
        1. 25.17.1 Advantages of Promises
        2. 25.17.2 Promises are not always the best choice
      18. 25.18 Cheat sheet: the ECMAScript 6 Promise API
        1. 25.18.1 Promise constructor
        2. 25.18.2 Static Promise methods
        3. 25.18.3 Promise.prototype methods
      19. 25.19 Further reading
  6. VI Miscellaneous
    1. 26. Unicode in ES6
      1. 26.1 Unicode is better supported in ES6
      2. 26.2 Escape sequences in ES6
        1. 26.2.1 Where can escape sequences be used?
        2. 26.2.2 Escape sequences in the ES6 spec
    2. 27. Tail call optimization
      1. 27.1 What is tail call optimization?
        1. 27.1.1 Normal execution
        2. 27.1.2 Tail call optimization
      2. 27.2 Checking whether a function call is in a tail position
        1. 27.2.1 Tail calls in expressions
        2. 27.2.2 Tail calls in statements
        3. 27.2.3 Tail call optimization can only be made in strict mode
        4. 27.2.4 Pitfall: solo function calls are never in tail position
      3. 27.3 Tail-recursive functions
        1. 27.3.1 Tail-recursive loops
    3. 28. Metaprogramming with proxies
      1. 28.1 Overview
      2. 28.2 Programming versus metaprogramming
        1. 28.2.1 Kinds of metaprogramming
      3. 28.3 A first look at proxies
        1. 28.3.1 Function-specific traps
        2. 28.3.2 Intercepting method calls
        3. 28.3.3 Revocable proxies
        4. 28.3.4 Proxies as prototypes
        5. 28.3.5 Forwarding intercepted operations
      4. 28.4 Use cases for proxies
        1. 28.4.1 Tracing property accesses (get, set)
        2. 28.4.2 Warning about unknown properties (get, set)
        3. 28.4.3 Negative Array indices (get)
        4. 28.4.4 Data binding (set)
        5. 28.4.5 Accessing a restful web service (method calls)
        6. 28.4.6 Revocable references
        7. 28.4.7 Implementing the DOM in JavaScript
        8. 28.4.8 Other use cases
      5. 28.5 The design of the proxy API
        1. 28.5.1 Stratification: keeping base level and meta level separate
        2. 28.5.2 Virtual objects versus wrappers
        3. 28.5.3 Transparent virtualization and handler encapsulation
        4. 28.5.4 The meta object protocol and proxy traps
        5. 28.5.5 Enforcing invariants for proxies
      6. 28.6 FAQ: proxies
        1. 28.6.1 Where is the enumerate trap?
      7. 28.7 Reference: the proxy API
        1. 28.7.1 Creating proxies
        2. 28.7.2 Handler methods
        3. 28.7.3 Invariants of handler methods
        4. 28.7.4 Operations that affect the prototype chain
        5. 28.7.5 Reflect
      8. 28.8 Conclusion
      9. 28.9 Further reading
    4. 29. Coding style tips for ECMAScript 6
    5. 30. An overview of what’s new in ES6
      1. 30.1 Categories of ES6 features
      2. 30.2 New number and Math features
        1. 30.2.1 New integer literals
        2. 30.2.2 New Number properties
        3. 30.2.3 New Math methods
      3. 30.3 New string features
      4. 30.4 Symbols
        1. 30.4.1 Use case 1: unique property keys
        2. 30.4.2 Use case 2: constants representing concepts
        3. 30.4.3 Pitfall: you can’t coerce symbols to strings
        4. 30.4.4 Which operations related to property keys are aware of symbols?
      5. 30.5 Template literals
      6. 30.6 Variables and scoping
        1. 30.6.1 let
        2. 30.6.2 const
        3. 30.6.3 Ways of declaring variables
      7. 30.7 Destructuring
        1. 30.7.1 Object destructuring
        2. 30.7.2 Array destructuring
        3. 30.7.3 Where can destructuring be used?
      8. 30.8 Parameter handling
        1. 30.8.1 Spread operator (...)
      9. 30.9 Callable entities in ECMAScript 6
      10. 30.10 Arrow functions
      11. 30.11 New OOP features besides classes
        1. 30.11.1 New object literal features
        2. 30.11.2 New methods in Object
      12. 30.12 Classes
      13. 30.13 Modules
        1. 30.13.1 Multiple named exports
        2. 30.13.2 Single default export
        3. 30.13.3 Browsers: scripts versus modules
      14. 30.14 The for-of loop
      15. 30.15 New Array features
      16. 30.16 Maps and Sets
        1. 30.16.1 Maps
        2. 30.16.2 Sets
        3. 30.16.3 WeakMaps
      17. 30.17 Typed Arrays
      18. 30.18 Iterables and iterators
        1. 30.18.1 Iterable values
        2. 30.18.2 Constructs supporting iteration
      19. 30.19 Generators
        1. 30.19.1 Implementing iterables via generators
        2. 30.19.2 Blocking on asynchronous function calls
      20. 30.20 New regular expression features
      21. 30.21 Promises for asynchronous programming
        1. 30.21.1 Chaining then() calls
        2. 30.21.2 Executing asynchronous functions in parallel
        3. 30.21.3 Glossary: Promises
      22. 30.22 Metaprogramming with proxies
© 2015 - 2016 Axel Rauschmayer (cover by Fran Caye)
This book is dedicated to the impossible girl, who has taught me so much about love and life.