Preface

Experienced programmers familiar with languages such as Java, C#, C, and C++ often find themselves in a position where they need to work with JavaScript. User interfaces are increasingly web-based, and JavaScript is the lingua franca of the web browser. The Electron framework extends this capability to rich client applications, and there are multiple solutions for producing mobile JavaScript apps. Increasingly, JavaScript is used on the server side.

Many years ago, JavaScript was conceived as a language for “programming in the small,” with a feature set that can be confusing and error-prone for larger programs. However, current standardization efforts and tool offerings go far beyond those humble beginnings.

Unfortunately, it is difficult to learn modern JavaScript without getting bogged down with obsolete JavaScript. Most books, courses, and blog posts are focused on transitioning from older JavaScript versions, which is not helpful for migrants from other languages.

That is the issue that this book addresses. I assume that you, the reader, are a competent programmer who understands branches and loops, functions, data structures, and the basics of object-oriented programming. I explain how to be productive with modern JavaScript, with only parenthetical remarks about obsolete features. You will learn how to put modern JavaScript to use, while avoiding pitfalls from the past.

JavaScript may not be perfect, but it has shown itself to be well-suited for user interface programming and many server-side tasks. As Jeff Atwood said presciently: “Any application that can be written in JavaScript, will eventually be written in JavaScript.”

Work through this book, and learn how to produce the next version of your application in modern JavaScript!

Five Golden Rules

If you avoid a small number of “classic” features of JavaScript, you can greatly reduce the mental load of learning and using the language. These rules probably won’t make sense to you right now, but I list them here for your future reference, and to reassure you that they are few in number.

  1. Declare variables with let or const, not var.

  2. Use strict mode.

  3. Know your types and avoid automatic type conversion.

  4. Understand prototypes, but use modern syntax for classes, constructors, and methods.

  5. Don’t use this outside constructors or methods.

And a meta-rule: Avoid the Wat—those snippets of confusing JavaScript code followed by a sarcastic “Wat?!” Some people find delight in demonstrating the supposed awfulness of JavaScript by dissecting obscure code. I have never learned anything useful from going down that rabbit hole. For example, what is the benefit of knowing that 2 * ['21'] is 42 but 2 + ['40'] is not, if the golden rule #3 tells you not to mess with type conversions? In general, when I run into a confusing situation, I ask myself how to avoid it, not how to explain its gory but useless details.

The Learning Paths

When I wrote the book, I was trying to put information where you can find it when you need it. But that’s not necessarily the right place when you read the book for the first time. To help you customize your learning path, I tag each chapter with an icon that indicates its basic level. Sections that are more advanced than the chapter default get their own icons. You should absolutely skip those sections until you are ready for them.

Here are the icons:

The impatient rabbit denotes a basic topic that even the most impatient reader should not skip.

Images

Alice indicates an intermediate topic that most programmers want to understand, but perhaps not on first reading.

Images

The Cheshire cat points to an advanced topic that puts a smile on the face of a framework developer. Most application programmers can safely ignore these.

Images

Finally, the mad hatter labels a complex and maddening topic, intended only for those with morbid curiosity.

Images

A Tour of the Book

In Chapter 1, we get going with the basic concepts of JavaScript: values and their types, variables, and most importantly, object literals. Chapter 2 covers control flow. You can probably skim over it quickly if you are familiar with Java, C#, or C++. In Chapter 3, you will learn about functions and functional programming, which is very important in JavaScript. JavaScript has an object model that is very different from class-based programming languages. Chapter 4 goes into detail, with a focus on modern syntax. Chapters 5 and 6 cover the library classes that you will most often use for working with numbers, dates, strings, and regular expressions. These chapters are largely at the basic level, with a sprinkling of more advanced sections.

The next four chapters cover intermediate level topics. In Chapter 7, you will see how to work with arrays and the other collections that the standard JavaScript library offers. If your programs interact with users from around the world, you will want to pay special attention to the coverage of internationalization in Chapter 8. Chapter 9 on asynchronous programming is very important for all programmers. Asynchronous programming used to be quite complex in JavaScript, but it has become much simpler with the introduction of promises and the async and await keywords. JavaScript now has a standard module system that is the topic of Chapter 10. You will see how to use modules that other programmers have written, and to produce your own.

Chapter 11 covers metaprogramming at an advanced level. You will want to read this chapter if you need to create tools that analyze and transform arbitrary JavaScript objects. Chapter 12 completes the coverage of JavaScript with another advanced topic: iterators and generators, which are powerful mechanisms for visiting and producing arbitrary sequences of values.

Finally, there is a bonus chapter, Chapter 13, on TypeScript. TypeScript is a superset of JavaScript that adds compile-time typing. It is not a part of standard JavaScript, but it is very popular. Read this chapter to decide whether you want to stick with plain JavaScript or use compile-time types.

The purpose of this book is to give you a firm grounding of the JavaScript language so that you can use it with confidence. However, you will need to turn elsewhere for the ever-changing landscape of tools and frameworks.

Why I Wrote This Book

JavaScript is one of the most used programming languages on the planet. Like so many programmers, I knew a bit of pidgin JavaScript, and one day, I had to learn serious JavaScript in a hurry. But how?

There are any number of books that teach a little bit of JavaScript for casual web developers, but I already knew that much JavaScript. Flanagan’s Rhino book1 was great in 1996, but now it burdens readers with too many accidents from the past. Crockford’s JavaScript: The Good Parts2 was a wake-up call in 2008, but much of its message has been internalized in subsequent changes to the language. There are many books that bring old-style JavaScript programmers into the world of modern standards, but they assume an amount of “classic” JavaScript that was out of my comfort zone.

Of course, the web is awash in JavaScript-themed blogs of varying quality—some accurate but many with a tenuous grasp of the facts. I did not find it effective to scour the web for blogs and gauge their levels of truthfulness.

Oddly enough, I could not find a book for the millions of programmers who know Java or a similar language and who want to learn JavaScript as it exists today, without the historical baggage.

So I had to write it.

1. David Flanagan, JavaScript: The Definitive Guide, Sixth Edition (O’Reilly Media, 2011).

2. Published by O’Reilly Media, 2008.

Acknowledgments

I would like to once again thank my editor Greg Doench for supporting this project, as well as Dmitry Kirsanov and Alina Kirsanova for copyediting and typesetting the book. My special gratitude goes to the reviewers Gail Anderson, Tom Austin, Scott Davis, Scott Good, Kito Mann, Bob Nicholson, Ron Mak, and Henri Tremblay, for diligently spotting errors and providing thoughtful suggestions for improvements.

Cay Horstmann
Berlin
March 2020

Register your copy of Modern JavaScript for the Impatient on the InformIT site for convenient access to updates and/or corrections as they become available. To start the registration process, go to informit.com/register and log in or create an account. Enter the product ISBN (9780136502142) and click Submit. Look on the Registered Products tab for an Access Bonus Content link next to this product, and follow that link to access any available bonus materials. If you would like to be notified of exclusive offers on new editions and updates, please check the box to receive email from us.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.149.242