Book Image

Learning ECMAScript 6

By : Narayan Prusty
Book Image

Learning ECMAScript 6

By: Narayan Prusty

Overview of this book

<p>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. By learning the latest version of ECMAScript, you'll have a greater understanding of JavaScript and more confidence and fluency when developing with it - follow this book and use and adopt ES6 features into your work, instead of the usual tired JavaScript hacks and tricks.</p> <p>The book begins by introducing ECMAScript 6's built-in objects and &nbsp;shows you how to create custom Iterators. &nbsp;It also provides you with guidance on Next, how to write asynchronous code in a synchronous style using ES6, so you can unlock greater control and sophistication in the way you develop with JavaScript.</p> <p>Beyond this, you will also learn&nbsp;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.&nbsp;Finally, it explains old modular programming techniques such as IIFE, CommonJS, AMD, and UMD and also compares it with ECMAScript modules and demonstrates how modules can increase the performance of websites when used.</p>
Table of Contents (16 chapters)

Generators


A generator function is like a normal function, but instead of returning a single value, it returns multiple values one by one. Calling a generator function doesn't execute its body immediately, but rather returns a new instance of the generator object (that is, an object that implements both, iterable and iterator protocols).

Every generator object holds a new execution context of the generator function. When we execute the next() method of the generator object, it executes the generator function's body until the yield keyword is encountered. It returns the yielded value, and pauses the function. When the next() method is called again, it resumes the execution, and then returns the next yielded value. The done property is true when the generator function doesn't yield any more value.

A generator function is written using the function* expression. Here is an example to demonstrate this:

function* generator_function()
{
  yield 1;
  yield 2;
  yield 3;
  yield 4;
  yield 5;
}

let...