Preface

Even though this is my second book on technologies in the JavaScript ecosystem, I still find myself somewhat surprised at my role as a JavaScript expert and evangelist. Like so many programmers, I held a strong prejudice against JavaScript up until about 2012. To do such an about-face still feels a little disorienting.

My prejudice was for the usual reasons: I considered JavaScript a “toy” language (without really learning it properly, and therefore not knowing of what I spoke) that was practiced by dangerous, sloppy, untrained amateur programmers. There is a little truth in both of these reasons. JavaScript was developed quickly, and even its inventor Brendan Eich admits there are things that he didn’t get right the first time around—and by the time he realized it, too many people were relying on the problematic behavior for him to effectively change it (show me the language that doesn’t suffer from this problem, however). As for the second reason, JavaScript did make programming suddenly accessible. Not only did everyone have a browser, but with only a little effort, they could see the JavaScript that enabled the websites that were rapidly proliferating on the Web. People learned by trial and error, by reading each other’s code and—in so many cases—emulating poorly written code with insufficient understanding.

I’m glad I have learned enough about JavaScript to recognize that—far from being a toy language—it is based on extremely solid foundations, and is powerful, flexible, and expressive. I’m also glad I have come to embrace the accessibility that JavaScript brings. I certainly hold no animosity toward amateurs: everyone has to start somewhere, programming is a profitable skill, and a career in programming has many advantages.

To the new programmer, the amateur, I say this: there is no shame in being an amateur. There is some shame in staying an amateur (if you make programming your profession, certainly). If you want to practice programming, practice it. Learn everything you can, from every source you can. Keep an open mind and—perhaps most importantly—question everything. Question every expert. Question every experienced programmer. Constantly ask “Why?”

For the most part, I have tried to keep this book to the “facts” of JavaScript, but it is impossible to completely avoid opinion. Where I offer opinions, take them for what they are. You are welcome to disagree, and you are encouraged to seek out the opinions of other experienced developers.

You are learning JavaScript at a very exciting time. The Web is leaving its infancy (technically speaking), and web development isn’t the confusing, complicated Wild West that it was 5 and 10 years ago. Standards like HTML5 and ES6 are making it easier to learn web development, and easier to develop high-quality applications. Node.js is extending the reach of JavaScript beyond the browser, and now it is a viable choice for system scripting, desktop application development, backend web development, and even embedded applications. Certainly I haven’t had this much fun programming since I started in the mid-1980s.

A Brief History of JavaScript

JavaScript was developed by Brendan Eich, a developer at Netscape Communications Corporation, in 1995. Its initial development was very rapid, and much of the criticism leveled at JavaScript has cited the lack of planning foresight during its development. However, Brendan Eich was not a dabbler: he had a solid foundation in computer science, and incorporated remarkably sophisticated and prescient ideas into JavaScript. In many ways, it was ahead of its time, and it took 15 years for mainstream developers to catch on to the sophistication the language offered.

JavaScript started life with the name Mocha, and was briefly named LiveScript before being officially renamed to JavaScript in a Netscape Navigator release in 1995. The word “Java” in “JavaScript” was not coincidental, but it is confusing: aside from a common syntactic ancestry, JavaScript has more in common with Self (a prototype-based language developed at Xerox PARC in the mid-’80s) and Scheme (a language developed in the 1970s by Guy Steele and Gerald Sussman, which was in turn heavily influenced by Lisp and ALGOL) than with Java. Eich was familiar with both Self and Scheme, and used some of their forward-thinking paradigms in developing JavaScript. The name JavaScript was partially a marketing attempt to tie into the success Java was enjoying at the time.1

In November 1996, Netscape announced that they had submitted JavaScript to Ecma, a private, international nonprofit standards organization that carries significant influence in the technology and communications industries. Ecma International published the first edition of the ECMA-26 specification, which was, in essence, JavaScript.

The relationship between Ecma’s specifications—which specify a language called ECMAScript—and JavaScript is mostly academic. Technically, JavaScript is an implementation of ECMAScript, but for practical purposes, JavaScript and ECMAScript can be thought of interchangeably.

The last major ECMAScript version was 5.1 (generically referred to as ES5), published in June 2011. Browsers “in the wild” that are old enough not to support ECMAScript 5.1 have fallen well below the single digits, and it’s safe to say that ECMAScript 5.1 is the current lingua franca of the Web.

ECMAScript 6 (ES6)—which is the focus of this book—was published by Ecma International in June 2015. The working name for the specification prior to publication was “Harmony,” and you will hear ES6 referred to as “Harmony,” “ES6 Harmony,” “ES6,” “ES2015,” and “ECMAScript 2015.” In this book, we will refer to it simply as ES6.

ES6

If ES5 is the current lingua franca of the Web, the attentive reader might be wondering why this book focuses on ES6.

ES6 represents a significant advancement in the JavaScript language, and some of ES5’s major shortcomings are addressed in ES6. I think you will find that ES6 is generally a much more pleasant and powerful language to work with (and ES5 was quite enjoyable to start with). Also—thanks to transcompilers—you can write ES6 today and transcompile it to “web-compatible” ES5.

With ES6 finally published, browser support for it will grow steadily, and at some point, transcompilation will no longer be necessary to reach a broad audience (I am not foolish enough to make a prediction—even a rough one—about when that will happen).

What’s clear is that ES6 represents the future of JavaScript development, and by investing your time in learning it now, you will be prepared for the future, with transcompilers preventing us from sacrificing portability now.

However, not every developer will have the luxury of writing ES6 today. It’s possible that you’re working on a very large existing ES5 code base that would be prohibitively expensive to convert to ES6. And some developers simply won’t wish to go through the extra effort involved in transcompilation.

With the exception of Chapter 1, this book will cover ES6, not ES5. Where appropriate, I will point out where ES6 differs from ES5, but there will not be side-by-side code examples, or extensive discussion of doing things “the ES5 way” when there is a better way in ES6. If you fall into that category of programmers who, for whatever reason, need to stick to ES5, this may not be the book for you (though I hope you will return to it at some point in the future!).

The editorial choice to focus on ES6 was made carefully. The improvements in ES6 are significant enough that it would have been difficult to maintain a clear pedagogical framework. In short, a book that attempts to cover ES5 and ES6 would do both topics a disservice.

Who This Book Is For

This book is primarily for readers who already have some experience with programming (even an introductory programming class, or an online course). If you’re new to programming, this book will be helpful, but you might want to supplement it with an introductory text or class.

Those who already have some JavaScript experience (especially if it’s only in ES5) will find a practical and thorough coverage of important language concepts.

Programmers who are coming from another language should feel right at home with the content in this book.

This book does attempt to comprehensively cover the language features, related tools, techniques, and paradigms that drive modern JavaScript development. Therefore, the material in this book necessarily ranges from the simple and straightforward (variables, control flow, functions) to the complicated and esoteric (asynchronous programming, regular expressions). Depending on your level of experience, you may find some chapters more challenging than others: the beginning programmer will no doubt need to revisit some of the material more than once.

What This Book Is Not

This book is not a comprehensive reference to JavaScript or its related libraries. The Mozilla Developer Network (MDN) maintains an excellent, thorough, up-to-date, and free online JavaScript reference, which will be referenced liberally throughout this book. If you prefer a physical book, David Flanagan’s JavaScript: The Definitive Guide is quite comprehensive (though it does not cover ES6 at the time of this writing).

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

O’Reilly Safari

Note

Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/learningJS_3E.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

The opportunity to write books for O’Reilly is a tremendous honor, and I owe a debt of gratitude to Simon St. Laurent for seeing the potential in me, and bringing me on board. Meg Foley, my editor, has been supportive, encouraging, and ever-helpful (there’s a certain green T-shirt in the mail for you, Meg!). An O’Reilly book is a team effort, and my copyeditor Rachel Monaghan, production editor Kristen Brown, and proofreader Jasmine Kwityn were fast, thorough, and insightful: thank you all for your effort!

To my technical reviewers—Matt Inman, Shelley Powers, Nick Pinkham, and Cody Lindley—thanks for your astute feedback, for your brilliant ideas, and for helping make this book great. To say I couldn’t have done it without you is an understatement. While everyone’s feedback was incredibly helpful, I want to give special recognition to Matt: his experience as an educator provided valuable insight on all matters of pedagogy, and the liberal use of Stephen Colbert images in his feedback helped me maintain my sanity!

Shelley Powers—author of previous editions of this book—deserves special thanks not just for passing this title on to me, but for providing her experienced feedback and for making this a better book (and for sparking some spirited discussions!).

I would like to thank all the readers of my previous book (Web Development with Node and Express). If you hadn’t bought that book—and reviewed it so positively!—I would probably not have had the opportunity to write this book. Special thanks to the readers who took the time to send in feedback and corrections: I have learned a lot from your responses!

To everyone at Pop Art, where I am honored to work: you are my rock. Your support humbles me, your enthusiasm motivates me, and your professionalism and dedication are what get me out of bed in the morning. Tom Paul in particular deserves my gratitude: his unwavering principles, innovative business ideas, and exceptional leadership inspire me not only to do my best today, but to do even better tomorrow. Thanks to Steve Rosenbaum for founding Pop Art, weathering stormy seas, and successfully passing the torch on to Tom. As I have taken time to finish this book, Colwyn Fritze-Moor and Eric Buchmann worked extra hard to cover duties that I normally would have handled: thank you both. Thanks to Dylan Hallstrom for being the very model of reliability. Thanks to Liz Tom and Sam Wilskey for joining Team Pop Art! Thanks to Carole Hardy, Nikki Brovold, Jennifer Erts, Randy Keener, Patrick Wu, and Lisa Melogue for all of your support. Lastly, thanks always to my predecessors, from whom I learned so much: Tony Alferez, Paul Inman, and Del Olds.

My enthusiasm for this book—and the subject of programming languages in particular—was sparked by Dr. Dan Resler, Associate Professor at Virginia Commonwealth University. I registered for his class on compiler theory with a singular lack of interest, and left that class with a passion for formal language theory. Thank you for passing your enthusiasm—and some small part of your depth of understanding—on to me.

Thanks to all of my friends in the PSU part-time MBA cohort—it’s been such a pleasure getting to know you all! Special thanks to Cathy, Amanda, Miska, Sahar, Paul S., Cathy, John R., Laurie, Joel, Tyler P., Tyler S., and Jess: you’ve all enriched my life so much!

If my coworkers at Pop Art motivate me to greatness and inspire my days, my friends motivate me to deepness and light up my nights. Mark Booth: no friend knows me better, and there is no one I would sooner trust my deepest secrets to. Your creativity and talent put me to shame: don’t let me show you up with this stupid book. Katy Roberts is as reliable as the incoming tide, and as beautiful. Katy, thank you for your deep and abiding kindness and friendship. Sarah Lewis: I love your face. Byron and Amber Clayton are true and loyal friends who always bring a smile to my face. Lorraine, it’s been years, but you still bring out the best in me. To Kate Nahas: I’m so glad we have reconnected after so many years; I look forward to sharing a toast to Duke’s memory. To Desember: thank you for your trust, warmth, and companionship. Lastly, thanks to my new friends Chris Onstad and Jessica Rowe: you two have brought so much joy and laughter into my life in the last two years, I don’t know what I would have done without you.

To my mother, Ann: thank you for your unwavering support, love, and patience. My father, Tom, remains my prototype for curiosity, innovation, and dedication, and without him I would be a poor engineer (or perhaps not an engineer at all). My sister, Meris, will always be a fixed point in my life, representing loyalty and conviction.

1 Eich confessed in a 2014 interview to enjoying thumbing his nose at Sun Microsystems, who “hated JavaScript.”

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

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