Expert Javascript Expert Javascript Is Your Definitive Guide to Understanding How and Why Javascript Behaves the Way It Does
Total Page:16
File Type:pdf, Size:1020Kb
BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Daggett RELATED Expert JavaScript Expert JavaScript is your definitive guide to understanding how and why JavaScript behaves the way it does. Master the inner workings of JavaScript by learning in detail how modern applications are made. In covering lesser-understood aspects of this powerful language and truly understanding how it works, your JavaScript code and programming skills will improve. You will learn about core fundamentals of JavaScript, including deep dives into functions, scopes, closures, and practical object-oriented code. Mark Daggett explains clearly how closures, events, and asynchronous code really operate, as well as conventions and concepts to write JavaScript in a clear, pragmatic style. Many of the changes in ECMAScript6 and its implications are all explained. You’ll be introduced to modern workflow tools to make application development faster, more enjoyable, and ostensibly more profitable. You’ll understand how to measure code quality and write more testable JavaScript, and finally you’ll learn about real-world applications of JavaScript, including JavaScript- powered robots. JavaScript is one of the most powerful languages on the web today, and it is only getting stronger. This book will take you through the process of planning, coding, testing, profil- ing and finally releasing your application, at expert level. With more frameworks and more improvements than ever, now is the time to become an expert at JavaScript. Make this journey - use Expert JavaScript today. What you’ll Learn: • What is really going on underneath functions, in arguments, types, coercion, and scope • How closures, events, and asynchronous code work at a fundamental level • How to understand advanced topics including promise objects, coroutines, and generators • How to apply this newfound knowledge pragmatically to build the very best modern JavaScript applications Shelve in Web Development/JavaScript ISBN 978-1-4302-6097-4 User level: Advanced SOURCE CODE ONLINE 9 781430260974 www.apress.com www.allitebooks.com Download from Wow! eBook <www.wowebook.com> For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.allitebooks.com Contents at a Glance About the Author ���������������������������������������������������������������������������������������������������������������xiii About the Technical Reviewer �������������������������������������������������������������������������������������������� xv Acknowledgments ������������������������������������������������������������������������������������������������������������ xvii Introduction ����������������������������������������������������������������������������������������������������������������������� xix ■ Chapter 1: Objects and Prototyping �����������������������������������������������������������������������������������1 ■ Chapter 2: Functions �������������������������������������������������������������������������������������������������������31 ■ Chapter 3: Getting Closure �����������������������������������������������������������������������������������������������47 ■ Chapter 4: Jargon and Slang �������������������������������������������������������������������������������������������57 ■ Chapter 5: Living Asynchronously �����������������������������������������������������������������������������������79 ■ Chapter 6: JavaScript IRL ����������������������������������������������������������������������������������������������107 ■ Chapter 7: Style �������������������������������������������������������������������������������������������������������������131 ■ Chapter 8: Workflow ������������������������������������������������������������������������������������������������������151 ■ Chapter 9: Code Quality �������������������������������������������������������������������������������������������������175 ■ Chapter 10: Improving Testability ���������������������������������������������������������������������������������199 Index ���������������������������������������������������������������������������������������������������������������������������������219 v www.allitebooks.com Introduction In my mind, good technical books are part mixtape, treasure map, and field journal. Expert JavaScript is the result of my efforts to successfully weave these forms together into a compelling and information-rich book about JavaScript. A mixtape, for those old enough to remember, is a curated collection of songs. These tapes were often made as gifts for friends, lovers, and those in between. The mixer would craft the tape by selecting personal favorites or organizing tracks along a conceptual thread. Often these tapes were a surrogate for the mixer, a way to be remembered by the listener when the tape was playing. This book is a mixtape for JavaScript that I made for you. These chapters cover some of my favorite aspects of the language, but also includes less-understood topics because they are not easily explained in a tweet or blog post. The long form format of a book affords these subjects the necessary room to breathe. As a child, I found the idea of finding a treasure map a thrilling prospect. I was captivated by the idea that anyone could become rich as long as they followed the map. This book will not lead you to buried treasure, but it is a map of sorts. I laid out these chapters to chart the inner workings of the language, which you can follow to the end. Dig through these concepts with me and you will unearth a deeper understanding of JavaScript than when you started. A field journal is kept by scientists. They are taught to keep a log of their thoughts, observations, and hunches about their subject. They may even tape leaves, petals, or other artifacts of nature between its pages. It’s a highly contextual diary about a subject of study filtered through a specific point of view. The purpose of the field journal is to be a wealth of information that the scientist can continually mine when they are no longer in the field. Expert JavaScript is my field journal of JavaScript, which I wrote to return to often. I will use it to help me remember and understand the particulars of the language. I encourage you to do the same. Scribble in the margins, highlight sections, and bookmark pages. It is not a precious object; it is meant to be a living document that is improved through your use. xix www.allitebooks.com CHAPTER 1 Objects and Prototyping Practice does not make perfect. Only perfect practice makes perfect. —Vince Lombardi It may seem odd to include three chapters on core concepts of JavaScript in a book for experts. After all, these topics are some of the most rudimentary components of the language. My assertion is this: just as a person can speak a language without the ability to read or write it, so too can developers use the fundamental features of JavaScript and yet be blissfully unaware of their complexities. The goal of these chapters is to shine a light on some of the more shadowy portions of the language. These are the concepts that you may have always intended to learn or even assumed you already understood. Think of it as if you are descending into your brain’s basement, in which JavaScript is stored. Use this text like a flashlight to check for cracks in the foundation of your knowledge. This chapter and the next are meant to fill any fissures that might be revealed. Do not think of it as a needless review, but rather a structural assessment of your understanding of JavaScript. I will start with a high-level overview of the goals of the language. But before you know it, you will be flat on your belly, commando-crawling your way through the lesser-known concepts of JavaScript. I will describe in detail the important ideas related to objects and prototypes. Then, in the next chapters you’ll look at functions and closures, which are the building blocks of JavaScript. JavaScript from a Bird’s-Eye View What we call JavaScript is actually an implementation of the ECMAScript language specification. For JavaScript to be considered a valid version of ECMAScript, it must provide mechanisms to support the syntax and semantics defined in the spec. JavaScript as an implementation must provide the programmer affordances to use the various types, properties, values, functions, and reserved words that make up ECMAScript. Once a version of JavaScript conforms to ECMAScript, language designers are free to embellish their version with extra features and methods as they see fit. The ECMAScript specification explicitly allows this kind of flourish, as you can read here: A conforming implementation of ECMAScript is permitted to provide additional types, values, objects, properties, and functions beyond those described in this specification. In particular, a conforming implementation of ECMAScript is permitted to provide properties not described in this specification, and values for those properties, for objects that are described in this specification. A conforming implementation of ECMAScript is permitted to support program and regular expression syntax not described in this specification. 1 www.allitebooks.com CHAPTER 1 ■ OBJECTS AND PROTOTYPING The fact that these extra features can exist in parallel with the core elements and still be considered a valid implementation is a sign of how progressive the ECMAScript standards body is. The looseness of what qualifies as ECMAScript is simultaneously a benefit and a drawback.