You Dont Know JS: Types & Grammar
by Kyle Simpson
Copyright 2015 Getify Solutions, Inc. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editors: Simon St. Laurent and Brian MacDonald
- Production Editor: Kristen Brown
- Copyeditor: Christina Edwards
- Proofreader: Charles Roumeliotis
- Interior Designer: David Futato
- Cover Designer: Ellie Volckhausen
- February 2015: First Edition
Revision History for the First Edition
- 2015-01-23: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491904190 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. You Dont Know JS: Types & Grammar, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-90419-0
[LSI]
Foreword
It was once said, JavaScript is the only language developers dont learn to use before using it.
I laugh each time I hear that quote because it was true for me and I suspect it was for many other developers. JavaScript, and maybe even CSS and HTML, were not among the core computer science languages taught at college in the Internets early days, so personal development was very much based on the budding developers search and view source abilities to piece together these basic web languages.
I still remember my first high school website project. The task was to create any type of web store, and me being a James Bond fan, I decided to create a Goldeneye store. It had everything: the Goldeneye MIDI theme song playing in the background, JavaScript-powered crosshairs following the mouse around the screen, and a gunshot sound that played upon every click. Q would have been proud of this masterpiece of a website.
I tell that story because I did back then what many developers are doing today: I copied-and-pasted chunks of JavaScript code into my project without having a clue about whats actually happening. The widespread use of JavaScript toolkits like jQuery have, in their own small way, perpetuated this pattern of not learning of core JavaScript.
Im not disparaging JavaScript toolkit use; after all, Im a member of the MooTools JavaScript team! But the reason JavaScript toolkits are as powerful as they are is because their developers know the fundamentals, and their gotchas, and apply them magnificently. As useful as these toolkits are, its still incredibly important to know the basics of the language, and with books like Kyle Simpsons You Dont Know JS series, theres no excuse not to learn them.
Types & Grammar, the third installment of the series, is an excellent look at the core JavaScript fundamentals that copy-and-paste and JavaScript toolkits dont and could never teach you. Coercion and its pitfalls, natives as constructors, and the whole gamut of JavaScript basics are thoroughly explained with focused code examples. Like the other books in this series, Kyle cuts straight to the point, with no fluff and wordsmithingexactly the type of tech book I love.
Enjoy Types & Grammar and dont let it get too far away from your desk!
David Walsh (http://davidwalsh.name), Senior Web Developer at Mozilla
Preface
Im sure you noticed, but JS in the series title is not an abbreviation for words used to curse about JavaScript, though cursing at the languages quirks is something we can probably all identify with!
From the earliest days of the Web, JavaScript has been a foundational technology that drives interactive experience around the content we consume. While flickering mouse trails and annoying pop-up prompts may be where JavaScript started, nearly two decades later, the technology and capability of JavaScript has grown many orders of magnitude, and few doubt its importance at the heart of the worlds most widely available software platform: the Web.
But as a language, it has perpetually been a target for a great deal of criticism, owing partly to its heritage but even more to its design philosophy. Even the name evokes, as Brendan Eich once put it, dumb kid brother status next to its more mature older brother Java. But the name is merely an accident of politics and marketing. The two languages are vastly different in many important ways. JavaScript is as related to Java as Carnival is to Car.
Because JavaScript borrows concepts and syntax idioms from several languages, including proud C-style procedural roots as well as subtle, less obvious Scheme/Lisp-style functional roots, it is exceedingly approachable to a broad audience of developers, even those with little to no programming experience. The Hello World of JavaScript is so simple that the language is inviting and easy to get comfortable with in early exposure.
While JavaScript is perhaps one of the easiest languages to get up and running with, its eccentricities make solid mastery of the language a vastly less common occurrence than in many other languages. Where it takes a pretty in-depth knowledge of a language like C or C++ to write a full-scale program, full-scale production JavaScript can, and often does, barely scratch the surface of what the language can do.
Sophisticated concepts that are deeply rooted into the language tend instead to surface themselves in seemingly simplistic ways, such as passing around functions as callbacks, which encourages the JavaScript developer to just use the language as-is and not worry too much about whats going on under the hood.
It is simultaneously a simple, easy-to-use language that has broad appeal, and a complex and nuanced collection of language mechanics that without careful study will elude true understanding even for the most seasoned of JavaScript developers.
Therein lies the paradox of JavaScript, the Achilles heel of the language, the challenge we are presently addressing. Because JavaScript can be used without understanding, the understanding of the language is often never attained.
Mission
If at every point that you encounter a surprise or frustration in JavaScript, your response is to add it to the blacklist (as some are accustomed to doing), you soon will be relegated to a hollow shell of the richness of JavaScript.
While this subset has been famously dubbed The Good Parts, I would implore you, dear reader, to instead consider it the The Easy Parts, The Safe Parts, or even The Incomplete Parts.
This You Dont Know JS series offers a contrary challenge: learn and deeply understand