Contents in Detail
UNDERSTANDING ECMASCRIPT 6
The Definitive Guide for JavaScript Developers
Nicholas C. Zakas
San Francisco
UNDERSTANDING ECMASCRIPT 6. Copyright 2016 by Nicholas C. Zakas.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
Printed in USA
First printing
20 19 18 17 16 1 2 3 4 5 6 7 8 9
ISBN-10: 1-59327-757-1
ISBN-13: 978-1-59327-757-4
Publisher: William Pollock
Production Editor: Alison Law
Cover Illustration: Garry Booth
Interior Design: Octopod Studios
Developmental Editor: Jennifer Griffith-Delgado
Technical Reviewer: Juriy Zaytsev
Copyeditor: Anne Marie Walker
Proofreader: James Fraleigh
Indexer: BIM Creatives, LLC
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 415.863.9900;
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
A catalog record of this book is available from the Library of Congress.
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
About the Author
Nicholas C. Zakas has been working on web applications since 2000, focusing on frontend development, and is known for writing and speaking about frontend best practices. He honed his experience during his five years at Yahoo!, where he was principal frontend engineer for the Yahoo! home page. He is the author of several books, including The Principles of Object-Oriented JavaScript (No Starch Press, 2014) and Professional JavaScript for Web Developers (Wrox, 2012).
About the Technical Reviewer
Juriy Zaytsev (known online as kangax) is a frontend web developer based in New York. Hes been exploring and writing about the quirky nature of JavaScript since 2007. Juriy has contributed to several open source projects, including Prototype.js and other popular projects like his own Fabric.js. He co-founded an on-demand custom print service called printio.ru and currently works at Facebook.
BRIEF CONTENTS
CONTENTS IN DETAIL
BLOCK BINDINGS
STRINGS AND REGULAR EXPRESSIONS
FUNCTIONS
EXPANDED OBJECT FUNCTIONALITY
DESTRUCTURING FOR EASIER DATA ACCESS
SYMBOLS AND SYMBOL PROPERTIES
SETS AND MAPS
ITERATORS AND GENERATORS
INTRODUCING JAVASCRIPT CLASSES
IMPROVED ARRAY CAPABILITIES
PROMISES AND ASYNCHRONOUS PROGRAMMING
PROXIES AND THE REFLECTION API
ENCAPSULATING CODE WITH MODULES
A
MINOR CHANGES IN ECMASCRIPT 6
B
UNDERSTANDING ECMASCRIPT 7 (2016)
FOREWORD
ECMAScript 6 has taken the world by storm. It came long after people stopped waiting for it, and then it spread faster than most people could learn it. Everybody has a different story about it. Here is mine.
In 2013, I worked at a startup that pivoted from iOS to the web. It was before I co-created Redux or participated in the JavaScript open source community. At the time, I was struggling to learn web development, and I was terrified. My team had to build a web version of our product from scratch in just a few months. In JavaScript.
At first I scoffed at the idea of writing something large in JavaScript. But a new team member persuaded me that JavaScript was not a toy language. I agreed to give it a try. I set my prejudices aside, opened MDN and StackOverflow, and learned JavaScript in depth for the first time. The simplicity I discovered enchanted me. My colleague also taught me how to use tools such as a linter and a bundler. In a few weeks, I woke up and realized that I enjoyed writing JavaScript.
But no language is perfect. I missed the frequent updates that Id come to expect after working with other languages. The only substantial update to JavaScript in a decade, ECMAScript 5, was a mere cleanup that nevertheless took years for browsers to fully support. At the time, the upcoming ECMAScript 6 (ES6) specification, codenamed Harmony, was far from finished and seemed like a distant future. Maybe in 10 years Ill get to write some ES6 code, I thought.
There were some experimental transpilers like Google Traceur that translated code from ES6 into ES5. Most of them were very limited or hard to plug into an existing JavaScript build pipeline. But then a new transpiler called 6to5 came along and changed everything. It was easy to install, integrated well with the existing tools, and produced readable code. It spread like wildfire. Now called Babel, 6to5 brought ES6 features to a mainstream audience even before the specification was finalized. In a matter of months, ES6 was everywhere.
ES6 has divided the community for a number of reasons. As this book goes to press, it is still not fully implemented in many major browsers. Having a build step can be intimidating when youre just learning the language. Some libraries have documentation and examples in ES6, and you might wonder if it is possible to use those libraries in ES5 at all. This contributes to the confusion. Many people didnt expect any new features in the language because it had almost never changed before. Others anxiously awaited the new features arrival and used all of them togetherin some cases beyond what was necessary.
Just as I was becoming proficient with JavaScript, I felt that somebody pulled the rug from under my feet, and now I had to learn a new language. I felt bad about this for a few months. Finally, on Christmas Eve, I started reading a draft of this book. I couldnt put it down. Next thing I knew, it was 3 AM, everybody at the party was asleep, and I understood ES6!
Nicholas is an incredibly gifted teacher. He conveys deep details in a straightforward way so they dont go over your head. Apart from this book, he is also known for creating ESLint, a JavaScript code analyzer that has been downloaded millions of times.
Nicholas knows JavaScript like very few people do. Dont miss the chance to soak up some of his knowledge. Read this book, and you, too, will become confident in your understanding of ES6.