You Dont Know JS: ES6 & Beyond
by Kyle Simpson
Copyright FILL IN YEAR Getify Solutions, Inc.. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( http://safaribooksonline.com ). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editors: Simon St. Laurent and Brian MacDonald
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
Revision History for the First Edition
- 2015-05-07: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491904244 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. You Dont Know JS: ES6 & Beyond, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the author(s) have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-90424-4
[FILL IN]
Foreword
Kyle Simpson is a thorough pragmatist.
I cant think of higher praise than this. To me, these are two of themost important qualities that a software developer must have. Thatsright: must, not should. Kyles keen ability to tease apart layersof the JavaScript programming language and present them inunderstandable and meaningful portions is second to none.
ES6 & Beyond will be familiar to readers of the You Dont Know JSseries: they can expect to be deeply immersed in everything from theobvious, to the very subtlerevealing semantics that were either takenfor granted or never even considered. Until now, the You Dont KnowJavaScript book series has covered material that has at least somedegree of familiarity to its readers. They have either seen or heardabout the subject matter; they may even have experience with it. Thisentry covers material that only a very small portion of the JavaScriptdeveloper community has been exposed to: the evolutionary changes to thelanguage introduced in the ECMAScript 2015 Language Specification.
Over the last couple years, Ive witnessed Kyles tireless efforts tofamiliarize himself with this material to a level of expertise that isrivaled by only a handful of his professional peers. Thats quite afeat, considering that at the time of this writing, the languagespecification document hasnt been formally published! But what Ivesaid is true, and Ive read every word that Kyles written for thisbook. Ive followed every change, and each time, the content only getsbetter and provides yet a deeper level of understanding.
This book is about shaking up your sense of understanding by exposingyou to the new and unknown. The intention is to evolve your knowledge instep with your tools by bestowing you with new capabilities. It existsto give you the confidence to fully embrace the next major era ofJavaScript programming.
Rick Waldron [@rwaldron](http://twitter.com/rwaldron) Open Web Engineerat Bocoup Ecma/TC39 Representative for jQuery
Chapter 1. ES? Now & Future
Before reading this book, I assume you have a solid working proficiencyover JavaScript up to the most recent standard (at the time of thiswriting), which is commonly called ES5 (technically ES 5.1). Here, weplan to talk squarely about the upcoming ES6, as well as cast ourvision beyond to understand how JS will evolve moving forward.
If you are still looking for confidence with JavaScript, I highlyrecommend you read the other titles in this series first:
Up & Going: Are you new to programming and JS? This is the roadmapyou need to consult as you start your learning journey.
Scope & Closures: Did you know that JS lexical scope is based oncompiler (not interpreter!) semantics? Can you explain how closures area direct result of lexical scope and functions as values?
this & Object Prototypes: Can you recite the four simple rules forhow this
is bound? Have you been muddling through fake classes in JSinstead of adopting the simpler behavior delegation design pattern?Ever heard of OLOO?
Types & Grammar: Do you know the built-in types in JS, and moreimportantly do you know how to properly and safely use coercion betweentypes? How comfortable are you with the nuances of JS grammar/syntax?
Async & Performance: Are you still using callbacks to manage yourasynchrony? Can you explain what a promise is and why/how it solvescallback hell, and how generators improve the legibility of asynccode? What exactly constitutes mature optimization of JS programs andindividual operations?
If youve read all those titles and you feel pretty comfortable withthose topics, its time we dive into the evolution of JS to explore allthe changes coming not only soon but farther over the horizon.
ES6 is not just a modest set of new APIs added to the langauge, as ES5was. It incorporates a whole slew of new syntactic forms, some of whichmay take quite a bit of getting used to. Theres also a variety of neworganization forms and new API helpers for various data types.
ES6 is a radical jump forward for the language. Even if you think youdo know JS in ES5, ES6 is full of new stuff you dont know yet, soget ready! This book will explore all the major themes of ES6 that youneed to get up to speed on, and even gaze at future features you shouldbe aware of that are coming down the track.
Warning
All code in this book assumes an ES6+ environment. At thetime of this writing, ES6 support varies quite a bit in browsers and JSenvironments (like node.js), so your mileage may vary.
Versioning
The JavaScript standard is referred to officially as ECMAScript(abbreviated ES), and up until just recently has been versionedentirely by ordinal number (i.e., 5 for 5th edition).
The earliest versions ES1 and ES2 were not widely known or implemented,but ES3 was the first widespread baseline for JavaScript. ES3constitutes the JavaScript standard for browsers like IE6-8 and olderAndroid 2.x mobile browsers. For politicial reasons beyond what wellcover here, the ill-fated ES4 never came about.
In 2009, ES5 was officially finalized (later ES5.1 in 2011), and settledas the widespread standard for JS for the modern revolution andexplosion of browsers, such as Firefox, Chrome, Opera, Safari, and manyothers.
Leading up to the expected next version of JS (slipped from 2013 to2014 and then 2015), the obvious and common label in discourse has beenES6.
However, late into the ES6 specification timeline, suggestions havesurfaced that versioning may in the future switch to being year-based,such ES2016 (aka ES7) to refer to whatever version of the specificationis finalized before the end of 2016. Some disagree, but ES6 will likelymaintain its dominant mindshare over the late change substitute ES2015.However ES2016 may in fact signal the new versioning scheme.