JavaScript: Namespaces

Feb 13 by Andre
The more things you build in Javascript, the more you will need to partition them into namespaces. There are (at least) two reasons for this:
  • so your functions/objects don't conflict with other peoples code. If you're writing code you intend to distribute, or if you are utilizing anyone else's code, you want to ensure none of the functions have the same name. Javascript will happily replace your setCookie() function with someone else's setCookie() function without so much as mentioning it to you. Problems like this are hard to track down. Namespacing your code is the solution.
  • to organize your functions/objects into groupings that make sense. If you were programming in Java, you wouldn't put all your classes in the same package, right?

How to namespace

In Javascript, you can namespace functions by making them properties of an object. Here's an example, which namespaces an augment() function I will utilize later:

var EC = new Object(); // EC is the first-level namespace, short for EarthCode
EC.F = new Object(); // F is the second-level namespace, short for foundation.

// now define the augment function itself
EC.F.augment = function (oSelf, oOther) {
  if (oSelf == null) {
    oSelf = {};
  }
  for (var i = 1; i < arguments.length; i++) {
    var o = arguments[i];
    if (typeof(o) != 'undefined' && o != null) {
      for (var j in o) {
        oSelf[j] = o[j];
      }
    }
  }
  return oSelf;
}
Now, in real usage, you will be placing multiple functions in a namespace. Here's the syntax I use:
// first, declare the two namespaces if they do not already exist
if (EC == null || typeof(EC) != "object") { var EC = new Object();}
if (EC.F == null || typeof(EC.F) != "object") { EC.F = new Object();}

// all the functions in the EC.F namespace will go in this block
EC.F = {
  augment: function (oSelf, oOther) {
    // contents of the augment function
  },
  Observer: function () {
    // contents of the Observer class
  },
  thirdFunction: function () {
    // function contents
  }
} // end of the EC.F namespace


Tips on namespace names
  • Make 'em short. You're going to be typing the "full path" to the function a lot in your code, and if it's long and a pain to type, you'll revert back to declaring everything in the root namespace.
  • Keep the whole path short. In Java it's common practice to name your packages "com.mycompany.etc.etc.," but this is unnecessary in Javascript. Again, you will be typing the full path a lot, so you want to keep it brief and utilitarian. Most of my namespaces are two packages deep, i.e, "EC.F.Augment()".
  • Plan for future growth. For example, if you're creating a pop-up notifier class, do you anticipate making other kinds of notifiers? Is it worthwhile placing it in a "notifier" package?

Comments

1

Oscar on Feb 13

Keeping the path too short is a maintenance nightmare waiting to happen, especially if you want other folks to read your code. Don't make them so short that you can't make sense of what it does or where it comes from (or worse have to over comment your code).

2

Web Femme on Apr 04

You should take a look at Ajile [ http://ajile.sf.net/ ]. It makes it really easy to create and work with namespaces in JavaScript. It even does the work of checking a namespace's existence for you. Really helps avoid those collisions.

Post a comment

 
This is so filters can reject the spam-bots. Thanks!