L E V E L U P Y O U R J A V A S C R I P T ECMAScript 6 represents the biggest update to the • How to improve asynchronous programming with core of JavaScript in the history of the language. promises In Understanding ECMAScript 6, expert developer • How modules change the way you organize code Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes Whether you’re a web developer or a Node.js that ECMAScript 6 brings to JavaScript. Every chap- developer, you’ll find Understanding ECMAScript 6 ter is packed with example code that works in any indispensable on your journey from ECMAScript 5 JavaScript environment so you’ll be able to see new to ECMAScript 6. features in action. You’ll learn: A B O U T T H E A U T H O R • How ECMAScript 6 class syntax relates to more Nicholas C. Zakas has been working on web appli- familiar JavaScript concepts cations since 2000, focusing on frontend develop- • What makes iterators and generators useful ment, and is known for writing and speaking about frontend best practices. He honed his experience • How arrow functions differ from regular functions during his five years at Yahoo!, where he was principal • Ways to store data with sets, maps, and more frontend engineer for the Yahoo! home page. He is the author of several books, including The Principles • The power of inheritance of Object-Oriented JavaScript (No Starch Press) and Professional JavaScript for Web Developers (Wrox). TH E F I N EST I N G E E K E NTE RTA I N M E NT ™ www.nostarch.com “I L I E F LAT .” $34.95 ($40.95 CDN) This book uses a durab le b ind ing that won’t snap shut. FSC FPO Z A K A S U N D E R S TA N D I N G E C M A S C R I P T 6 SHELVE IN: PROGRAMMING LANGUAGES/ JAVASCRIPT Understanding eCMasCript 6 U n d e r s t a n d i n g e C M a s C r i p t 6 t h e d e f i n i t i v e g u i d e f o r J a v a s c r i p t d e v e l o p e r s by 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; about the author Nicholas C. Zakas has been working on web applications since 2000, focusing on frontend development, and is known for writing and speak- ing 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. He’s 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. B r i e f C o n t e n t s Foreword by Dan Abramov xvii Acknowledgments xix Introduction xxi Chapter 1: Block Bindings 1 Chapter 2: Strings and Regular Expressions 13 Chapter 3: Functions 35 Chapter 4: Expanded Object Functionality 67 Chapter 5: Destructuring for Easier Data Access 83 Chapter 6: Symbols and Symbol Properties 99 Chapter 7: Sets and Maps 119 Chapter 8: Iterators and Generators 137 Chapter 9: Introducing JavaScript Classes 165 Chapter 10: Improved Array Capabilities 191 Chapter 11: Promises and Asynchronous Programming 213 Chapter 12: Proxies and the Reflection API 243 Chapter 13: Encapsulating Code with Modules 283 Appendix A: Minor Changes in ECMAScript 6 299 Appendix B: Understanding ECMAScript 7 (2016) 305 Index 311