The Javascript Dollar Sign ($) Function

Recently, I’ve been seeing a lot of Javascript functions prefixed with a dollar sign ($) or simply named $. I thought, what sort of magical property does the dollar sign have in Javascript? What sort of esotericism had I uncovered?

The Dollar Sign Magician

I checked the O’Reilly Rhinoceros book, and found that the dollar sign has no special properties.

However, I have come to discover that dollar sign function has become the more-or-less de facto shortcut to document.getElementById(). Let’s face it, we all use document.getElementById() a lot. Not only does it take time to type, but it adds bytes to your code as well.

Here’s the version from prototype.js, which can return more than just a single element:

function $(element) {
  if (arguments.length > 1) {
    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
      elements.push($(arguments[i]));
    return elements;
  }
  if (Object.isString(element))
    element = document.getElementById(element);
  return Element.extend(element);
}

Because it has become a common shortcut to document.getElementById(), it means that various Javascript libraries should play nicely together: if the dollar sign function in one .js file is overwritten by the same dollar sign function in another .js file, everything should still work. That also means that creating your own dollar sign function that does something different is probably not a good idea.

In case you were wondering, the underscore has no special properties either. But let’s face it, $(‘myVal’) looks more like programming than _(‘myVal’).

Update 2012
By now, the most common use of the $() function is as synonym for jQuery() in the popular jQuery Javascript framework. It returns a jQuery object (or objects), which includes much more than just a reference to the DOM element. (In jQuery, you can still access the underlying DOM element using .get().)

Likewise, the Prototype code I cited above also returns more than just a reference to a DOM element, as it returns an extended version of the element. As such, Prototype and jQuery will almost certainly not work well together in the majority of cases.

Welcome, Chris Petersen!

My friend and former co-worker, Chris Petersen, is going to be contributing to this blog as well.

Like me, he’s a bit of an accidental developer. However, he also has a master’s degree in information science from the University of Michigan School of Information, so he’ll be able to share his expertise on subjects like prototyping and usability testing. He develops primarily in ColdFusion.

Does releasing code to the public inspire extra effort?

About 2 years ago, I created a javascript image/photo gallery to embed gallery functionality into a web site using only client-side processing. Although I created it for a specific purpose, it was generic enough that I decided to make it available for other people to use on their sites.

Imagine my surprise when people took me up on that offer.

Continue reading Does releasing code to the public inspire extra effort?

Object Overhead in ColdFusion

I have been trying to improve the speed of a ColdFusion application. It was written by a part-timer, a Java developer who was new to ColdFusion. He wrote the code almost entirely in cfscript, and he used CFC objects extensively. In fact, it seems that he overused CFC objects—to the point where the server slows to a crawl.

Continue reading Object Overhead in ColdFusion

Clever ways to save bandwidth

When I worked for MLive.com, one of our practices was to leave out quotes around attribute values (except for alt attributes). This was to save on bandwidth costs. In spite of the fact that the page was no longer valid HTML (technically), all target browsers rendered the pages properly.

That might seem like nitpicking, but with over a million pageviews a day, all that ASCII can add up. Personally, I would prefer valid HTML, but I bet the financial side of that decision was pretty interesting. Continue reading Clever ways to save bandwidth

Version Control

As an alternative to a formal version control system, our development and even our production environments are littered with files like index12112006.html or index12202006.html. This is terrible.

Although we have had a CVS server set up since at least 2005 (it was there when I started), it is used inconsistently at best. Continue reading Version Control