Thoughts on JavaScript: The Good Parts

At a friend’s recommendation, I recently picked up Douglas Crockford’s classic book, JavaScript: The Good Parts:

Most programming languages contain good parts and bad parts. I discovered that I could be a better programmer by using only the good parts and avoiding the bad parts. After all, how can you build something good out of bad parts?

Chapter 1

The book only has a first edition, published in May 2008 – five years ago. Obviously, with the ES5 and forthcoming ES6–7 standards, several things have changed:

  • Globals, much bemoaned by Mr. Crockford, are now universally regarded as poor form. A few solutions for avoiding the problem exist today, and ES6 modules provide a good pattern for the future.
  • ES6’s let allows for block scoping.
  • Modifying built-in objects’ prototypes is quite common in the book. For example, from Chapter 4:

    String.method('trim', function () {
      return this.replace(/^\s+|\s+$/g, '');
    });
    

    This inserts a trim method on the String's prototype through a previously defined method function. Developers frown upon this sort of behavior because it modifies the runtime, leading to potential bugs. Libraries like Underscore.js are preferred.

  • All examples physically output to the DOM because console.log() didn’t exist at the time. Yikes.

  • The fifth chapter examines inheritance. I don’t fully understand all the patterns discussed, mostly because I see and use the constructor-prototype pattern most:

      var Mammal = function (name) {
          this.name = name;
      };
      Mammal.prototype.sayName = function () {
          return this.name;
      };
      var dog = new Mammal('fido');
      dog.sayName(); // fido
    

    ES6’s class provides syntactic sugar for this pattern, thus encouraging it. However, in a recent presentation, Mr. Crockford admits that he doesn't use this or Object.create() at all, instead showing this example:

    function constructor(spec) {
        var that = other_constructor(spec),
            member,
            method = function () {
                // spec, member, method
            };
        that.method = method;
        return that;
    }
    

    It seems rather theoretical to me: I don't understand its practical applications. But, I’m relatively new to the language.

  • The seventh chapter, which covers regular expressions, is the best source on the subject that I’ve read. It proves extremely informative to JavaScript’s often mysterious implementation.

JavaScript is in a far better place today, undoubtedly in part from this book. Yet, much has changed over the last five years; the book’s content shows its age. There are new good parts and new bad parts. I wish a second edition were in the works.

There’s still something to be gained: Mr. Crockford’s experience and critical approach on the subject is the best part. I highly recommend it for anyone who’s new to JavaScript.