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 brains 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 youll look at functions and closures, which are the building blocks of JavaScript.
JavaScript from a Birds-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.
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. Although the flexibility to add new features encourages language designers to innovate, it can leave developers in a bad spot trying to write clever polyfills to support the differences between the various implementations and runtime environments.
The ECMAScript specifications change over time, and occur for a variety of reasons (too many to enumerate here). Primarily, though, these changes are an attempt to codify new approaches to old problems or to support advancements in the larger computing ecosystem. The changing specification represents an attempt to formalize the evolutionary processes within the language. Therefore, although Im talking about core concepts as if they are immutable, in reality they are not. The concepts explored in this chapter are foundational and important, but my advice to the reader is to stay on your toes.
Scripting by Design
As its name implies, ECMAScript is a scripting language used to interact with a host environment programmatically. A host system, be it a browser, a server, or piece of hardware, exposes control points for JavaScript to manipulate. Most host environments allow JavaScript to trigger only aspects of the system that are already under the users control (albeit manually). For example, where a user of a browser might click a link on a web page using a mouse or finger, JavaScript could trigger the same event programmatically:
document.getElementById('search').click();
Traditionally, ECMAScript was almost exclusively intended as a tool for web scripting within browsers. Developers employed it to enhance the users experience when browsing a web page. Today, ECMAScript is equally at home on the server as it is in the browser, thanks to stand-alone engines such as V8 or TraceMonkey.
The ECMAScript standards body foresaw this growing divergence between how developers have traditionally used JavaScript, and where much of the recent growth has been. Wisely when defining what web scripting is in the most recent specification, it provided two examples that present the various contexts in which ECMAScript is popular today:
A web browser provides an ECMAScript host environment for client-side computation including, for instance, objects that represent windows, menus, pop-ups, dialog boxes, text areas, anchors, frames, history, cookies, and input/output. Further, the host environment provides a means to attach scripting code to events such as change of focus, page and image loading, unloading, error and abort, selection, form submission, and mouse actions. Scripting code appears within the HTML and the displayed page is a combination of user interface elements and fixed and computed text and images. The scripting code is reactive to user interaction and there is no need for a main program.
A web server provides a different host environment for server-side computation including objects representing requests, clients, and files; and mechanisms to lock and share data. By using browser-side and server-side scripting together, it is possible to distribute computation between the client and server while providing a customized user interface for a Web-based application.
Each Web browser and server that supports ECMAScript supplies its own host environment, completing the ECMAScript execution environment.
Note
At the time of this writing, the arrival of the newest version of ECMAScript 6 (named Harmony) was imminent, and although not officially released, many of the proposed changes are already being supported by runtime engines and browsers. This chapter is an exhaustive look at the core of the language, which also includes some of the new features introduced in Harmony. I will take special care to alert the reader when I am explaining a proposed feature that may have limited support.
Objects Overview
JavaScript is an object-oriented programming (OOP) language created by Brendan Eich, which he released after a few weeks of development while working for Netscape. Although JavaScript has Java in the name, it has little to do with the Java language. In an interview with InfoWorld , Eich explained the turn of events that lead to the language being renamed JavaScript:
InfoWorld: As I understand it, JavaScript started out as Mocha, then became LiveScript and then became JavaScript when Netscape and Sun got together. But it actually has nothing to do with Java or not much to do with it, correct?