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)

Preface

ECMAScript is a scripting language standardized by Ecma International in the ECMA-262 specification and ISO/IEC 16262. Scripting languages such as JavaScript, JScript and ActionScript are supersets of ECMAScript. Although JavaScript, JScript, and ActionScript have more capabilities than ECMAScript, by defining more objects and methods, the core features of these languages are the same as ECMAScript.

ECMAScript 6 is the sixth version and the seventh edition of the ECMAScript language. In short, it is also called "ES6".

Although JavaScript is extremely powerful and flexible, it's often criticized for having unnecessary redundancy. Therefore, JavaScript developers often use abstractions such as CoffeeScript and Typescript, which provide easier syntax, powerful features, and compile to JavaScript. ES6 was introduced to improve JavaScript and ensure that developers no longer needed to use abstractions or other techniques to write quality code, which was becoming a lengthy process.

The ES6 features are inherited from other popular and abstracting languages, such as CoffeeScript. Therefore, ES6 language features behave the same way as they do in other languages, and are not new in the programming world, even if they are new in JavaScript.

This book provides explanations with examples for all the features of the new version of ECMAScript, ECMAScript 6. This book is about the JavaScript implementation of ECMAScript 6. All the features and examples in this book work on all the JavaScript environments, such as browsers, Node.js, Cordova, and so on.

What this book covers

Chapter 1, Playing with Syntax, introduces new ways of creating variables and function parameters. This chapter discusses the new objects and functions syntaxes in more depth.

Chapter 2, Knowing Your Library, introduces the new prototype-based methods for the existing objects.

Chapter 3, Using Iterators, shows the different types of iterators available in ES6, and how to create custom iterators. It also discusses the tail call optimization in ES6.

Chapter 4, Asynchronous Programming, illustrates how Promises can make it easier to write code that's executed asynchronously.

Chapter 5, Implementing the Reflect API, gives an in-depth guide to object reflection in ES6.

Chapter 6, Using Proxies, shows how to define the custom behavior the fundamental operations on the objects using the ES6 proxies.

Chapter 7, Walking You Through Classes, introduces the Object-oriented Programming using the ES6 classes. Concepts such as inheritance, constructors, abstraction, information hiding, and more are explained here.

Chapter 8, Modular Programming, explains different ways to create modules using JavaScript. Technologies such as the IIFE, CommonJS, AMD, UMD, and ES6 modules are covered.

What you need for this book

If you are reading this book after ES6 is fully supported by all the JavaScript engines, then you don't need to set up any specific testing environment. You can simply test the examples on any engine of your choice.

If you are reading this book before ES6 is completely supported by all the JavaScript engines, then follow up with this book and execute the code snippets for which you can use an ES6 transpiler. If you want to run the code examples in the browser environment, then use this sample webpage template, which has Traceur transpiler attached to it for converting ES6 into ES5 on every page-load:

<!doctype html>
<html>
<head>...</head>
<body>
...

<script src="traceur.js"></script>
<script src="bootstrap.js"></script>
<script type="module">
            //Place ES6 code here
</script>
</body>
</html>

Download the traceur.js script from https://google.github.io/traceur-compiler/bin/traceur.js, and the bootstrap.js script from https://google.github.io/traceur-compiler/src/bootstrap.js. Then, place them in the same directory as the HTML file containing the previous code.

In the exercise files (the code bundle), Traceur transpiler and polyfills are already attached. The exercise files are created for testing the code examples on browsers.

For Chapter 4, Asynchronous Programming, you will have to use the browser environment for testing, as we have used jQuery and AJAX in the examples. You will also need a web server for it.

For Chapter 8, Modular Programming, if you use the browser environment for testing, then you need a web server. But if you use the Node.js environment, then you don't need a web server.

Compatibility with ECMAScript 6

This book was written before all the JavaScript engines started supporting all the features of ES6.

The specifications of ES6 have already been finalized. It's just not all JavaScript engines have finished the implementation of all the features of ES6. I am pretty much sure that by the end of 2016, all JavaScript engines will support ES6.

Kangax has created an ES6 compatibility table where you can track the support of various ES6 features on various JavaScript engines. You can find the table at http://kangax.github.io/compat-table/es6/.

Running ECMAScript 6 in incompatible engines

If you want to run ES6 in an engine that doesn't support ES6, then you can use the ES6 polyfills or the ES6 transpilers.

A polyfill is a piece of code that provides the technology that you, the developer, expect the JavaScript engine to provide natively. Remember that polyfills are not available for every ES6 feature, and that they cannot be created. A list of all the available polyfills and their download links are available at https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills#ecmascript-6-harmony.

An ES6 transpiler takes the ES6 source code and outputs the ES5 source code, which is compatible with all JavaScript engines. Transpilers support the conversion of more features than polyfills, but may not support all the features of ES6. There are various transpilers available, such as the Google Traceur (https://github.com/google/traceur-compiler), Google Caja (https://developers.google.com/caja/), Babel (https://babeljs.io/), Termi ES6 Transpiler (https://github.com/termi/es6-transpiler), and more. You should always transpile the ES6 code to ES5 before attaching it to your web pages, instead of transpiling it on frontend each time your page loads so that web pages don't load slower.

Therefore, by using a transpiler and/or polyfills, you can start writing ES6 code for distribution even before all the engines completely support ES6 and before non-ES6 engines become obsolete.

Who this book is for

This book is for anyone who is familiar with JavaScript. You don't have to be a JavaScript expert to understand this book. This book will help you take your JavaScript knowledge to the next level.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

var a = 12; //accessible globally

function myFunction()
{
  console.log(a);

  var b = 13; //accessible throughout function

  if(true)
  {
    var c = 14; //accessible throughout function
    console.log(b);
  }

  console.log(c);
}

myFunction();

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.