Skip to content

JavaScript needs namespaces

March 19, 2007

Writing small pieces of JavaScript is a rather simple and quick task. However, once you break through the 1000-line barrier things can get a little hairy. I’m not sure if the original designers of JavaScript thought much about large-scale JavaScript software projects but my guess is they didn’t. Because if they did they would have included one very important feature: namespaces.

I discovered this the hard way when I was trying to fix a perplexing bug in my MicroMenu script. I had a function called CreateMenuItem that seemed to be failing but all attempts to even debug the problem failed. After awhile I had a hunch that there must have been some clash between my script and the SharePoint JavaScript files. Sure enough, core.js also contains a CreateMenuItem function!

There are several ways to fix this, the simplest being a find & replace of CreateMenuItem in my code with something like MMCreateMenuItem. I could also encapsulate these methods into an object which creates an object namespace but involves more work. As a third option, the Yahoo UI library provides a namespace implementation.

If the web community decides to update JavaScript they must consider adding namespaces for all our sanity’s sake.

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: