Javascript: the problem with “$”
by admin on Jul.31, 2009, under General Discussion
Anyone who is familiar with Javascript and using many of the libraries like prototype, jquery and YUI are all to familiar with the power of “$”. Many of the new low level libraries such as these allow you to use the “$” as a shortcut to other functions already in the system. With prototype $ is short for “document.getElementByI” in jQuery it’s short for the “jQuery” object. Other libraries link it to whatever it is they feel it should be. This makes writing code a bit easier.
While it’s easier it’s also more problematic. Many people who don’t have time to write libraries rely on other’s libraries to do the work for them. They may need a carousel, or a modalbox, or they may have something that they wrote 3-4 years ago and defined “$” as something they wanted to define it as. This can be hugely problematic.
Let’s say I wrote a javascript application that used prototype. A while later my client who uses my javascript application finds a javascript modal that they really like that uses jQuery. The developer of that modal application uses “$” for jQuery. Now we have a conflict. I can’t just tell jQuery to not use “$”, even though it has that option because the modal application utilizes it heavily. Going through and modifying “$” to jQuery is a hassle. The solution should be simple, if you are writing a javascript application that you are planning on releasing publicly, you shouldn’t use shortcuts. You may keep some great website from using your work.