Errata

Learning JavaScript Design Patterns

Errata for Learning JavaScript Design Patterns, Second Edition

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Other Digital Version Section Object Creation (p. 63). Kindle Edition.
Code example 4

I think there is a small mistake in the code given in the book. It throws an error when I execute it in Firefox browser console: "Uncaught ReferenceError: config is not defined."


// 4. If this feels a little difficult to read, a short-hand could
// be written as follows:

var defineProp = function ( obj, key, value ){
config.value = value;
Object.defineProperty( obj, key, config );
};

Anonymous  Mar 24, 2024 
Mobi Page Page 32
The link in the last bullet point of the section "Anti-Patterns in JavaScript"

The code from the JSFiddle link in the last bullet point of the section "Anti-Patterns in JavaScript" is as follows:

```
function writeTest() {
var newHTML = "<div><h1>Hi there!</h1></div>";
document.write(newHTML);
document.close();
}

function elemTest() {
var newDiv = document.createElement("div");
newDiv.innerHTML = "<h1>Hi there!</h1>";
document.body.appendChild(newDiv);
}

writeTest();
```

Since the code executes before the page fully loads, both functions produce the same result, making it difficult to appreciate the difference between using document.write and document.createElement.

To better illustrate the intended point of the example, I have modified the code as follows:

```
function writeTest() {
var newHTML = "<div><h1>Hi there!</h1></div>";
document.write(newHTML);
document.close();
}

function elemTest() {
var newDiv = document.createElement("div");
newDiv.innerHTML = "<h1>Hi there!</h1>";
document.body.appendChild(newDiv);
}

window.addEventListener("load", (event) => {
writeTest();
});
```

With this adjustment, the difference between using document.write and document.createElement is clear. The writeTest function now overwrites the entire page content, in contrast to elemTest, which simply appends new content to the existing page.

I believe this change more accurately demonstrates the potential pitfalls of using document.write in modern web development.

Jorge Cancino  Jun 03, 2024 
Other Digital Version Chapter 7. Basic Constructors - Constructors with Prototypes
p.66

This error had already been submitted before, but it is still unconfirmed so I will also submit it.
The statement that a method defined in class will be redefined for each new instance is wrong. If it's a method within an object and you use a factory function for creating new objects then yes - you redefine the method function for each object. But writing method in a class is the same as writing method in prototype, because class is a syntactic sugar over prototypes. And with prototypes methods will be shared with all instances.

Karine Hiulumian  Aug 15, 2024 
Page 7. JavaScript Design Patterns - The Constructor Pattern - Basic Constructors
Last paragraph

The statement about methods defined in JavaScript Classes, like `toString()` in the code example, are redefined for each new object is wrong.

According to MDN web docs, 'methods are defined on the prototype of each class instance and are shared by all instances.'

One can assert the equality of the toString method for new objects using:`civic.toString === mondeo.toString // true`.

Kinlong Leung  Oct 11, 2023 
Printed Page 39
codesample and following headline + text

The codesample was updated from the first edition to reflect new features of ES2015 but the paragraph after the code was not updated. The statement, the toString-method is not shared between Car-instances, is wrong when using ES2015 classes. Also, the following headline and text is now irrelevant.

Alexander Schuc  Jun 21, 2023 
Printed Page 41
3rd example from the bottom

Output does not match implementation

Tom Elliott  Jun 11, 2023 
Printed Page 82
first heading on the page

Adjust the heading "Flyweight" to become "The Flyweight Pattern" for consistency between sections.

Anonymous  Aug 11, 2023