Tuesday, July 7, 2015

Book Review: Object-Oriented JavaScript, Second Edition

The Second Edition of “Object-Oriented JavaScript” (Packt Publishing, 2013) provides an excellent introduction to JavaScript and using JavaScript objects. The book, written by Stoyan Stefanov and Kumar Chetan Sharma, has the subtitle, “Learn everything you need to know about OOJS in this comprehensive guide.” This is a review of the Second Edition of Object-Oriented JavaScript, a book that features eight chapters spanning almost 300 pages and four appendices spanning nearly 50 pages.


The Preface of Object-Oriented JavaScript states that the book is for "anyone who is starting to learn JavaScript or who knows JavaScript but isn't very good at the object-oriented part of it." The Preface also states that readers need access to a modern web browser, text editor, and optional Node.js setup.

Chapter 1: Object-oriented JavaScript

The initial chapter of Object-Oriented JavaScript provides an overview of JavaScript, a brief summary of the history of JavaScript, a description of the current state of JavaScript, and some speculation on the future of JavaScript.

Chapter 1's coverage of ECMAScript 5 describes strict mode and the section on object-oriented programming introduces basic object-oriented concepts such as objects, classes, inheritance, encapsulation, aggregation, and polymorphism.

Chapter 1 ends with a discussion of "setting up your training environment" that introduces WebKit's Web Inspector, JavaScriptCore on the Mac, and consoles on different web browsers.

Chapter 2: Primitive Data Types, Arrays, Loops, and Conditions

Object-Oriented JavaScript's second chapter introduces the basics of JavaScript such as variables and data types, operators, comparisons, arrays, conditions, loops, switch statement, and comments. The chapter consists of 40+ pages and provides a nice introduction to JavaScript basics that should be approachable even for those relatively new to JavaScript.

Chapter 3: Functions

The third chapter of Object-Oriented JavaScript opens with a nice summary of the significance of functions in JavaScript:

Mastering functions is an important skill when you learn any programming language, and even more so when it comes to JavaScript. This is because JavaScript has many uses for functions, and much of the language's flexibility and expressiveness comes from them. Where most programming languages have a special syntax for some object-oriented features, JavaScript just uses functions.

Chapter 3's coverage of JavaScript functions introduces the basic syntax of a JavaScript function, how to write a function, and how to invoke a function. There is also a section of the chapter on predefined JavaScript functions such as parseInt(), parseFloat(), isNaN(), isFinite(), encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent(), eval(), and the non-standard alert().

Object-Oriented JavaScript's third chapter takes on one of JavaScript's trickiest areas: variable scope. It differentiates between "global" and "local" variables and describes variable hoisting. The chapter also introduces anonymous functions (including callback functions and immediate functions). The chapter concludes with coverage of JavaScript closures.

Chapter 4: Objects

The first three chapters of Object-Oriented JavaScript focus mostly on basic JavaScript with a little bit of focus on objects and object-oriented concepts in the first chapter. However, there is no significant discussion specific to the combination of the two (use of objects in JavaScript) until the fourth chapter. This chapter focuses on objects in JavaScript and begins by comparing and contrasting JavaScript objects to JavaScript arrays.

Chapter 4 of describes the two ways of accessing JavaScript object properties (square bracket notation and dot notation) before introducing constructor functions, use of the 'this' keyword, use of the 'instanceof' operator, comparing JavaScript objects, and passing objects to and returning objects from JavaScript functions.

The section of the fourth chapter on "objects in the WebKit console" demonstrates use of the WebKit console to view all of a JavaScript object's properties and use of console.log() and console.error() to view any desired JavaScript values. A section of Chapter 4 also focuses on built-in JavaScript objects Object, Array, Function, Boolean, Number, String, Math, Date, RegExp, and Error objects. The book features an appendix (Appendix C) with more details on more built-in objects.

Chapter 5: Prototype

The prototype property is the subject of the fifth chapter of Object-Oriented JavaScript. The chapter introduces and explains how the prototype property works and differentiates between "own properties" and "prototype properties." The chapter discusses enumerating an object's properties using a JavaScript for-in loop, Object.prototype.propertyIsEnumerable(), and Object.prototype.hasOwnProperty(). The chapter also describes Object.prototype.isPrototypeOf() and the "secret" __proto__ property.

The chapter's section "Augmenting built-in objects" describes adding new methods to built-in constructor functions. A sub-section also cautions about the use of this feature with some of the same arguments I discussed in the blog post A Java Developer's Perspective on the Power and Danger of JavaScript's Object Prototype. The chapter concludes with two "prototype gotchas."

Chapter 6: Inheritance

The sixth chapter of Object-Oriented JavaScript looks at "common patterns for implementing inheritance" in JavaScript. It discusses prototype chaining, copying prototypes, adding a constructor call, creating a so-called uber function to allow a child object to access its parent object, and encapsulating inheritance logic in an "extends" function.

A section in Chapter 6 discusses "inheritance" via "copy[ing] the properties you like from one object to another." It covers shallow copy and deep copy approaches as well as use of a custom object() function that "accepts an object and returns a new one that has the parent as a prototype" (prototypal inheritance/Object.create()). Other items related to inheritance discussed in this chapter include multiple inheritance, mixins, parasitic inheritance, and borrowing a constructor.

Chapter 6 ends with a table comparing the different approaches to JavaScript inheritance covered in the chapter. There is also analysis provided as an illustration of how to decide which approach is most suitable for different situations.

Chapter 7: The Browser Environment

Chapter 7 focuses on the historically most common deployment environment for JavaScript: the web browser. The chapter begins by demonstrating inclusion of JavaScript in an HTML page via external file and via direct embedding in the HTML. The chapter then introduces the W3C standard Document Object Model (DOM) and non-standard Browser Object Model (BOM), compares and contrasts them briefly, and dives into more details on each.

In another example of how Object-Oriented JavaScript provides introductory JavaScript details that are not necessarily specific to objects, the seventh chapter explains why feature sniffing (or capability detection) is generally preferred over browser detection. The chapter also uses illustrations and text to explain how the browser console can be used as "a cheat sheet" to "inspect [the BOM and DOM properties] in an object."

Several of the properties of the window object are introduced and briefly described in Chapter 7: window.location, window.history, window.frames, window.screen, window.open(), window.close(), window.moveTo(), window.resizeTo(), window.alert(), window.prompt(), window.confirm(), window.setTimeout(), window.setInterval(), and window.document. A section of the chapter explains the DOM and how to traverse it and style (CSS) elements on it.

Chapter 7 presents significant coverage of event handling and Ajax/XMLHttpRequest. Chapter 7, like Chapter 2 and Chapter 3 and most of Chapter 1, covers JavaScript in general rather than particularly focusing on objects in JavaScript. I found it interesting and a positive aspect of this book that only one chapter focused on JavaScript in the web browser and the remainder of the book treated JavaScript more generally.

Chapter 8: Coding and Design Patterns

The final chapter of the book introduces "coding patterns" (described in the chapter as "JavaScript-specific best practices") and "design patterns" (described in the chapter as "language-independent patterns, popularized by the famous 'Gang of Four' book"). The portion of the chapter covering "coding patterns" (or "best practices") discusses several ideas for improving one's JavaScript. Some of the items covered in this section include a very brief introduction to JSON, immediate functions, using modules, using namespaces, and separating behavior from content and presentation.

The second portion of Chapter 8 introduces design patterns as popularized by the book Design Patterns: Elements of Reusable Object-Oriented Software and covers four of the 23 patterns presented in that C++-oriented book from a JavaScript perspective. The four patterns covered in Object-Oriented JavaScript are Singleton, Factory, Decorator, and Observer.

The Appendices

A significant portion of Object-Oriented JavaScript is presented as four appendices. The appendices cover reserved words in JavaScript (keywords reserved for current use, keywords reserved for future use, and keywords that used to be but are no longer reserved), built-in JavaScript functions, built-in objects, and regular expressions in JavaScript. These appendices are presented largely in tabular format that makes them especially conducive as references. The chapters cover many of the same topics in more explanatory text and then the appendices provide quick reference on details associated with many of these topics.

General Observations

  • Most of the first chapter, Chapter 2, Chapter 3, Chapter 7, and half of Chapter 8 cover JavaScript in general rather than focusing on JavaScript objects. These chapters (or the first half of the chapter in the case of Chapter 8) could probably be skipped by developers familiar with the basics of JavaScript syntax who are mainly interested in learning specifically about using objects in JavaScript. For those who have little or no JavaScript experience, these chapters provide adequate introductory material that likely precludes the need of using any other introductory JavaScript book or resource before reading this book.
  • The chapters in Object-Oriented JavaScript tend to be explanatory text with illustrations while the appendices more concisely represent some of the same topics covered in the chapters in a reference format.
  • The PDF version of Object-Oriented JavaScript that I reviewed featured high-resolution color screen snapshots that helped illustrate points being made.
  • Code listings, even in the electronic edition I reviewed, are black text on white background with no line numbers and no color syntax.


Object-Oriented JavaScript (Second Edition) is a well-written book that thoroughly introduces object-oriented JavaScript, but also introduces many other basic JavaScript concepts and syntax. A reader with little or no familiarity with JavaScript is likely to gain much from this book because it has so many introductory JavaScript concepts covered in it. Even readers who have some experience and familiarity with basic JavaScript principles are likely to find the book helpful (especially chapters 4 through 6) for gaining a deeper understanding of JavaScript's take on object-oriented programming and application of JavaScript's prototype object.

No comments: