Preface

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 Flanigan’s 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 Didn’t 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 you’re 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).

<!DOCTYPE html><html lang="en"><body><script>

// this is a comment about a specific part of the code
var foo = 'calling out this part of the code';

</script></body></html>

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., console.log) will work in most any modern browser regardless of whether the browser has its own console. Before reading this book, make sure you are comfortable with the usage and purpose of console.log.

In situations where jsFiddle and Firebug lite-dev caused complications with the JavaScript code JS Bin and Firebug Lite-dev will be used. I’ve tried to avoid a dependency on a browser console by using Firebug lite-dev but with certain code examples the solution itself gets in the way of code execution. In these situations the console built into your web browser will have to be leveraged to output logs. If you are not using a browser with a built-in JavaScript console, I would suggest upgrading or switching browsers.

When JS Bin is used, keep in mind that the code has to be executed manually (clicking “Render”), which differs from the page load execution done by jsFiddle.

Conventions Used in This Book

The following typographical conventions are used in this book [see also Color-Coding Conventions]:

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 icon signifies a tip, suggestion, or general note.

Caution

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, if this book includes code examples, you may use the code in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “JavaScript Enlightenment by Cody Lindley (O’Reilly). Copyright 2013 Cody Lindley, 978-1-449-34288-3.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online (www.safaribooksonline.com) is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, 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, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

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://oreil.ly/javascript_enlightenment.

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

About the Author

Cody Lindley is a client-side engineer (a.k.a. front-end developer) and recovering Flash developer. He has an extensive background working professionally (11+ years) with HTML, CSS, JavaScript, Flash, and client-side performance techniques as it pertains to web development. If he is not wielding client-side code, he is likely toying with interface/interaction design or authoring material and speaking at various conferences. When not sitting in front of a computer, it is a sure bet he is hanging out with his wife and kids in Boise, Idaho—training for triathlons, skiing, mountain biking, road biking, alpine climbing, reading, watching movies, or debating the rational evidence for a Christian worldview.

About the Technical Editors

Michael Richardson

Michael Richardson is a web and application developer living in Boise, Idaho. Way back when, he got an MFA in creative writing from Sarah Lawrence and published a novel in 2003 called Plans for a Mushroom Radio. These days, when he’s not spending quality time with his lovely wife and rascal kid, he’s managing his little web-based application called Timeglider.

Kyle Simpson

Kyle Simpson is a JavaScript Systems Architect from Austin, Texas. He focuses on JavaScript, web performance optimization, and “middle-end” application architecture. If something can’t be done in JavaScript or web stack technology, he’s probably bored by it. He runs several open-source projects, including LABjs, HandlebarJS, and BikechainJS. Kyle works as a Software Engineer on the Development Tools team for Mozilla.

Nathan Smith

Nathan Smith is a UX developer at HP. He holds a MDiv from Asbury Theological Seminary. He began building sites late last century and enjoys hand coding HTML, CSS, and JavaScript. He created the 960 Grid System, a design and CSS framework for sketching, designing, and coding page layouts. He also made Formalize, a JavaScript and CSS framework that endeavors to bring sanity to form styling.

Ben Nadel

Ben Nadel is the chief software engineer at Epicenter Consulting, a Manhattan-based web application development firm specializing in innovative custom software that transforms the way its clients do business. He is also an Adobe Community Professional as well as an Adobe Certified Professional in Advanced ColdFusion. In his spare time, he blogs extensively about all aspects of obsessively thorough web application development at www.bennadel.com.

Ryan Florence

Ryan Florence is a front-end web developer from Salt Lake City, Utah, and has been creating websites since the early 90’s. He is especially interested in creating experiences that are pleasing to both the end user and the developer inheriting the project. Ryan is active in the JavaScript community writing plugins, contributing to popular JavaScript libraries, speaking at conferences and meet-ups, and writing about it on the web. He currently works as a Senior Technical Consultant at Clock Four.

Nathan Logan

Nathan Logan has been a professional web developer for eight years. His focus is on client-side technologies, but he also digs the server-side. He currently works for Memolane, alongside the author of this book. Personally, Nathan is blessed with a wonderful wife and son, and enjoys mountain biking, hot springs, spicy food, scotch, and Christian faith/theology.

Get JavaScript Enlightenment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.