Writing reusable JavaScript

One of the biggest hurdles in going from JavaScript as a trivial gimmick for making divs change color to a powerful language for building real applications is its single-namespace weakness. It’s easy to accidentally cause variable names to collide when you start combining scripts, and there’s no native library or module system to alleviate this. For example, jQuery, prototype.js, and MooTools all want to use “$” as a shortcut variable, which has caused me problems before when combining software written by other people.

It’s easy to write a bunch of JavaScript that drops variables all over the global namespace, but often, you want to reuse some JavaScript component in multiple pages. Let’s look at a hypothetical example:
Continue reading Writing reusable JavaScript