Thursday, May 7, 2009

JavaScript: The Good Parts


What's the most popular functional language? JavaScript is. And if you are interested in learning the finer points of the language I recommend Douglas Crockford's latest book titled JavaScript: The Good Parts. It is a very quick read at only 145 pages! The most valuable lesson I learned from the book was the correct way to create JavaScript objects with the Module Pattern. The following examples illustrate the Module pattern. This creational pattern leverages closure to achieve encapsulation, information hiding, and global safety. No other JavaScript creational pattern provides these capabilities. This should be your preferred creational pattern in JavaScript.

Module Pattern Examples:

/**
* A search module that provides the ability to search by name.
*/

var search = function (criteria) {
/* private variables */
var name = criteria.name;

/* private methods */
var renderResponse = function() {
alert('rendering response for: ' + name);
};

/* return our public API methods */
return {
execute: function() {
alert('searching for: ' + name); // Perform an AJAX search
renderResponse(); // Perform renderResponse as part of the AJAX callback.
}
};
};


Instantiate a new search object:
search({'name':'Johnny'}).execute();

Run search example


Alternatively, if you prefer to return your public API methods via assignment you may find this example to be simpler:

var search = function (criteria) {
/* private variables */
var that = {};
var name = criteria.name;

/* private methods */
var renderResponse = function() {
alert('rendering response for: ' + name);
};

/* Assign public API methods to that. */
that.execute = function () {
alert('searching for: ' + name); // Perform an AJAX search
renderResponse(); // Perform renderResponse as part of the AJAX callback.
}

return that; // return by assignment example.
};


Module Pattern Advantages

  • Improved encapsulation. For example, if the renderResponse functionality is only pertinent to search, we can encapsulate that behavior within the search object.
  • Better information hiding. No consumers of the search module will be able to call renderResponse directly because it is private.
  • The objects are global-safe. You will not see any references to the this keyword anywhere. This will eliminate any possibility of altering global variables.
  • Requires less effort than other creational patterns (pseudoclassical pattern, Object.create pattern).


JavaScript Resources:

  • JavaScript: The Good Parts presentation from the Google Code Blog.
  • In particular, I found the following sections within JavaScript: The Good Parts to be most valuable for me. These mere ten pages of content were worth the cost of the book!:
    • Chapter 4 (Functions):
      • Closure
      • Module
    • Chapter 5 (Inheritance):
      • Functional
  • JSLint: an excellent tool for code quality analysis.
  • Doug Crockford has at least eight JavaScript presentations available at YUI theatre