Simplifying JavaScript - The Pragmatic Programmer

Extracted from:

Simplifying JavaScript

Writing Modern JavaScript with ES5, ES6, and Beyond

This PDF file contains pages extracted from Simplifying JavaScript, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit . Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printed versions; the content is otherwise identical. Copyright ? 2018 The Pragmatic Programmers, LLC. All rights reserved.

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, or otherwise,

without the prior consent of the publisher.

The Pragmatic Bookshelf

Raleigh, North Carolina

Simplifying JavaScript

Writing Modern JavaScript with ES5, ES6, and Beyond Joe Morgan

The Pragmatic Bookshelf

Raleigh, North Carolina

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trademarks of The Pragmatic Programmers, LLC.

Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.

Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at .

The team that produced this book includes:

Publisher: Andy Hunt VP of Operations: Janet Furlow Managing Editor: Brian MacDonald Supervising Editor: Jacquelyn Carter Development Editor: Andrea Stewart Copy Editor: Nancy Rapoport Indexing: Potomac Indexing, LLC Layout: Gilson Graphics

For sales, volume licensing, and support, please contact support@.

For international rights, please contact rights@.

Copyright ? 2018 The Pragmatic Programmers, LLC.

All rights reserved.

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, or otherwise, without the prior consent of the publisher.

Printed in the United States of America. ISBN-13: 978-1-68050-288-6 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0--April 2018

Tip 41

Create Iterable Properties with Generators

In this tip, you'll learn how to convert complex data structures to iterables with generators. In Tip 14, Iterate Over Key-Value Data with Map and the Spread Operator, on page ?, you learned how simple it is to loop over maps thanks to iterables. And once you can iterate over a collection, you have access to the spread operator, array methods, and many other tools to transform your data. Iterables give your data more flexibility by allowing you to access each piece of data individually. You also know that objects don't have a built-in iterator. You can't loop over an object directly--you need to convert part of it to an array first. That can be a major problem when you want the structure of an object but the flexibility of an iterable. In this tip, you'll learn a technique that can make complex data structures as easy to use as simple arrays. You're going to use a new specialized function called a generator to return data one piece at time. In the process, you'll see how you can convert a deeply nested object into a simple structure. Generators aren't exclusive to classes. They're a specialized function. At the same time, they're very different from other functions. And while the JavaScript community has enthusiastically embraced most new features, they haven't quite figured out what to do with generators. In late 2016, a poll by Kent Dodds, a popular JavaScript developer, found that 81 percent of developers rarely or never used generators.4 That's changing. Developers and library authors are discovering how to use generators. One of the best use cases so far is to use generators to transform objects into iterables. What is a generator? The Mozilla Developer Network explains that a generator is a function that doesn't fully execute its body immediately when called.5

4. 5. *

? Click HERE to purchase this book now. discuss

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download