Web Development for 2010

Alexander Dickson - Web Developer covering PHP, jQuery / Javascript, XHTML, CSS, more

Should jQuery objects be prefixed with $?

Published on Friday, 11th June 2010.

About 6 months ago, I decided to prefix my jQuery objects with the dollar sign ($). I did this because I had seen it in the wild and it looked like a good idea at the time. Looking back now, I'm not sure if it is the best idea, or if I will continue to do it.

var $headerImages = $('#header img');

The ECMAScript specification also states used to state (thanks to Brian McKenna for the update) that the $ should only be used with machine generated code. Perhaps the proliferation of JavaScript libraries using the $ made the specification change to suit.

Pre-pending a dollar sign to all variables looks a lot like Hungarian Notation, or more specifically Systems Hungarian. Personally, I find this style of naming very ugly, and not very helpful. Some people think all variables should be prefixed with their type, but since JavaScript is loosely typed, it doesn't make much sense. Other programmers make good arguments against using Hungarian Notation in JavaScript.

The last thing that really irks me with the $ prefix is that it makes JavaScript variables look like PHP variables (or Perl).

I think I will forego the prefix for now, because it is non standardised and ugly. I'd like to hear your thoughts.

Comments

  • Russell Dias

    Posted on Friday, 11th June 2010 @ 11:42pm.

    Coming from a PHP background, at first I thought it was the best thing ever, but as I started dabbling in other languages (mainly Python) I realized that the $ sign is infact, very very annoying. Probably time consuming in the long run (actually I have no proof to back up this statement). I can't see myself doing the same thing in JS!

Leave a Comment

Comment Details

Your email will never be displayed. If you have a gravatar, it will be displayed.

Note: Your comment may require approval before it is posted to the site.

Stack Overflow Profile

view full profile »

About

I'm a web developer from the Sunshine Coast, Australia. more »