Introduction
This book is not about JavaScript design patterns or implementing an object-oriented paradigm with JavaScript code. It was not written to distinguish the good features of the JavaScript language from the bad. It is not meant to be a complete reference guide. It is not targeted at people new to programming or those completely new to JavaScript. Nor is this a cookbook of JavaScript recipes. Those books have been written.
It was my intention to write a book to give the reader an accurate JavaScript worldview through an examination of native JavaScript objects and supporting nuances: complex values, primitive values, scope, inheritance, the head object, etc. I intend this book to be a short and digestible summary of the ECMAScript 3 Edition specification, focused on the nature of objects in JavaScript.
If you are a designer or developer who has only used JavaScript under the mantle of libraries (such as jQuery, MooTools, Zepto, YUI, Dojo, etc.), it is my hope that the material in this book will transform you from a JavaScript library user into a JavaScript developer.
Why Did I Write This Book?
First, I must admit that I wrote this book for myself. Truth be told, I crafted this material so I could drink my own Kool-Aid and always remember what it tastes like. In other words, I wanted a reference written in my own words used to jog my memory as needed. Additionally:
Libraries facilitate a black box syndrome that can be beneficial in some regards, but detrimental in others. Things may get done fast and efficiently, but you have no idea how or why. And the how and why really matter when things go wrong or performance becomes an issue. The fact is that anyone who intends to implement a JavaScript library or framework when building a web application (or just a good signup form) ought to look under the hood and understand the engine. This book was written for those who want to pop the hood and get their hands dirty in JavaScript itself.
Mozilla has provided the most up-to-date and complete reference guide for JavaScript 1.5. I believe what is missing is a digestible document, written from a single point of view, to go along with their reference guide. It is my hope that this book will serve as a what you need to know manual for JavaScript values, detailing concepts beyond what the Mozilla reference covers.
Version 1.5 of JavaScript is going to be around for a fair amount of time, but as we move towards the new additions to the language found in ES5 and ES6, I wanted to document the cornerstone concepts of JavaScript that will likely be perennial.
Advanced technical books written about programing languages are often full of monolithic code examples and pointless meanderings. I prefer short explanations that get to the point, backed by real code that I can run instantly. I coined a term, technical thin-slicing, to describe what I am attempting to employ in this book. This entails reducing complex topics into smaller, digestible concepts taught with minimal words and backed with comprehensive/focused code examples.
Most JavaScript books worth reading are three inches thick. Definitive guides like David Flanigans certainly have their place, but I wanted to create a book that hones in on the important stuff without being exhaustive.
Who Should Read This Book?
This book is targeted at two types of people. The first is an advanced beginner or intermediate JavaScript developer who wishes to solidify his or her understanding of the language through an in-depth look at JavaScript objects. The second type is a JavaScript library veteran who is ready to look behind the curtain. This book is not ideal for newbies to programming, JavaScript libraries, or JavaScript itself.
Why JavaScript 1.5 and ECMAScript 3 Edition?
In this book, I focus on version 1.5 of JavaScript (equivalent to ECMAScript 3 Edition) because it is the most widely implemented version of JavaScript to date. The next version of this book will certainly be geared towards the up-and-coming ES5 and ES6.
Why Didnt I Cover the Date(), Error(), and RegEx() Objects?
Like I said, this book is not an exhaustive reference guide to JavaScript. Rather, it focuses on objects as a lens through which to understand JavaScript. So I have decided not to cover the Date()
, Error()
, or RegEx()
objects because, as useful as they are, grasping the details of these objects will not make or break your general understanding of objects in JavaScript. My hope is that you simply apply what you learn here to all objects available in the JavaScript environment.
Before you begin, it is important to understand various styles employed in this book. Please do not skip this section, because it contains important information that will aid you as you read the book.
More Code, Fewer Words
Please examine the code examples in detail. The text should be viewed as secondary to the code itself. It is my opinion that a code example is worth a thousand words. Do not worry if youre initially confused by explanations. Examine the code. Tinker with it. Reread the code comments. Repeat this process until the concept being explained becomes clear. I hope you achieve a level of expertise such that well-documented code is all you need to grok a programming concept.
Exhaustive Code and Repetition
You will probably curse me for repeating myself and for being so comprehensive with my code examples. And while I might deserve it, I prefer to err on the side of being exact, verbose, and repetitive, rather than make false assumptions authors often make about their reader. Yes, both can be annoying, depending upon what knowledge you bring to the subject, but they can also serve a useful purpose for those who want to learn a subject in detail.
Color-Coding Conventions
In the JavaScript code examples (example shown below), bold is used to highlight code directly relevant to the concept being discussed. Any additional code used to support the bold code will be in normal text. The lighter gray in the code examples is reserved for JavaScript comments (example shown below).
// this is a comment about a specific part of the codevar foo = 'calling out this part of the code';jsFiddle, JS Bin, and Firebug lite-dev
The majority of code examples in this book are linked to a corresponding jsFiddle page, where the code can be tweaked and executed online. The jsFiddle examples have been configured to use the Firebug lite-dev plugin so that the log function (i.e.,