Javascript the New Toys
Total Page:16
File Type:pdf, Size:1020Kb
JAVASCRIPT® THE NEW TOYS INTRODUCTION .................................................. xxxi CHAPTER 1 The New Toys in ES2015–ES2020, and Beyond ................. 1 CHAPTER 2 Block-Scoped Declarations: let and const .................... 17 CHAPTER 3 New Function Features ................................... 39 CHAPTER 4 Classes ................................................ 65 CHAPTER 5 New Object Features .................................... 105 CHAPTER 6 Iterables, Iterators, for-of, Iterable Spread, Generators ........ 131 CHAPTER 7 Destructuring .......................................... 165 CHAPTER 8 Promises .............................................. 181 CHAPTER 9 Asynchronous Functions, Iterators, and Generators ........... 221 CHAPTER 10 Templates, Tag Functions, and New String Features ........... 241 CHAPTER 11 New Array Features, Typed Arrays ......................... 263 CHAPTER 12 Maps and Sets ......................................... 293 CHAPTER 13 Modules .............................................. 319 CHAPTER 14 Reflection—Reflect and Proxy ............................ 365 CHAPTER 15 Regular Expression Updates .............................. 397 CHAPTER 16 Shared Memory ........................................ 417 CHAPTER 17 Miscellany ............................................. 461 CHAPTER 18 Upcoming Class Features ................................ 493 CHAPTER 19 A Look Ahead … ....................................... .517 APPENDIX Fantastic Features and Where to Find Them ................. 539 INDEX ........................................................... 557 www.allitebooks.com JavaScript® The New Toys www.allitebooks.com JavaScript® THE NEW TOYS T.J. Crowder www.allitebooks.com JavaScript®: The New Toys Copyright © 2020 by Thomas Scott “T.J.” Crowder Published simultaneously in Canada ISBN: 978-1-119-36797-6 ISBN: 978-1-119-36797-0 (ebk) ISBN: 978-1-119-36796-3 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2018965303 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. JavaScript is a registered trademark of Oracle America, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book. www.allitebooks.com To Wendy and James, who’ve met late nights and working weekends with unstinting support and loving encouragement. www.allitebooks.com ABOUT THE AUTHOR T.J. CROWDER is a software engineer with 30 years of professional experience, at least half that time working with JavaScript. He runs Farsight Software, a UK software consulting and product company. As one of the top 10 contributors on Stack Overflow and the top contributor in the JavaScript tag, he likes to use what he’s learned to help others with the technical challenges they’re facing, with an emphasis not just on imparting knowledge, but on helping with the process of solving problems. T.J. started programming in his early teens in California, playing around with the Apple II and Sinclair ZX-80 and -81 using BASIC and assembly language. He got his first proper job with comput- ers many years later, working a technical support position for a company with a PC (DOS) product for court reporters. While working support, he taught himself C from the TurboC manuals in the office, reverse-engineered the company’s undocumented, compressed binary file format, and used that knowledge to create a much-requested feature for the product in his spare time (one the com- pany soon started shipping). Before long the development department took him in and over the next several years gave him the opportunity, resources, and responsibility to grow into a programmer and, eventually, into their lead engineer, creating various products including their first Windows product. At his next company he took on a professional services and developer education role that saw him customizing the company’s enterprise product on-site for clients (using VB6, JavaScript, and HTML) and teaching training classes to the clients’ developers; eventually he was writing the training classes. A move from the U.S. to London put him back in a straight development role where he was able to increase his software design, SQL, Java, and JavaScript skills before branching out into independent contracting. Since then through circumstance he’s been doing primarily closed-source remote development work for a variety of companies and organizations (a NATO agency, a UK local government authority, and various private firms) working primarily in JavaScript, SQL, C#, and (recently) TypeScript. The desire for community led him first to the PrototypeJS mailing list back in the day, then to Stack Overflow, and now to various platforms. English and American by birth, American by upbringing, T.J. lives in a village in central England with his wife and son. ABOUT THE TECHNICAL EDITOR CHAIM KRAUSE is an expert computer programmer with over thirty years of experience to prove it. He has worked as a lead tech support engineer for ISPs as early as 1995, as a senior developer sup- port engineer with Borland for Delphi, and has worked in Silicon Valley for over a decade in various roles, including technical support engineer and developer support engineer. He is currently a military simulation specialist for the US Army’s Command and General Staff College, working on projects such as developing serious games for use in training exercises. He has also authored several video training courses on Linux topics, and has been a technical reviewer for over two dozen books. ABOUT THE TECHNICAL PROOFREADER MARCIA K. WILBUR is a technical communicator consulting in the semiconductor field, focusing on Industrial IoT (IIoT) and AI. Marcia holds degrees in computer science, technical communication, and information technology. As Copper Linux User Group president, she is heavily involved with the maker community leading West Side Linux + Pi and the East Valley leading regular Raspberry Pi, Bea- glebone, Banana Pi/Pro, and ESP8266 Projects including home automation, gaming consoles, surveil- lance, network, multimedia, and other “pi fun”. ACKNOWLEDGMENTS “I wrote a book” is almost never an accurate statement. Sure, all the unquoted words in the book are mine. But none of them would be here if it weren’t for others—others offering support, offering perspective, offering encouragement; others reviewing drafts to help cut the chaff and reveal the nugget of usefulness buried within; others asking questions in a variety of forums over the years, giving me the opportunity to practice the art and craft of explaining what meager knowledge I’ve been able to