ProfK
ProfK

Reputation: 51064

Is there an good reason to prefix all JavaScript modules with '$'?

In a project that recently landed on my plate, all client code is enclosed in revealing modules. This is good. All of the module names are prefixed with the '$' character, e.g.

$.acme.global.dataAccess = function () {
    var dataAccess = {};

None of these modules seems to be intended as a jQuery plugin, so I can imagine no good reason to do this at all, yet most of this code is well architected and written, so I might be missing something fundamental.

Upvotes: 4

Views: 184

Answers (1)

Todd
Todd

Reputation: 5454

IMO no. It's convenient, sure, because it's easy to type.

For this project, I say continue using it to maintain consistency. But...

The problem I feel is that there is an association between '$' and jquery, so confusion -- such as this -- is bound to arise. And when jquery is in use, you're slapping all of the "acme" stuff onto the "jQuery" object; there's no good reason for this unless it has something to do with jquery. You'd be just fine creating a new namespace. Why not Acme.global.dataAccess? It's even shorter, and there would be no immediate confusion of whether this module deals with JQuery or not.

TLDR

  • no, no particularly good reason to use it
  • it can be confusing
  • it does tack code onto the "jquery" object
  • yes, you can use it and..
  • for consistency, you should continue using it for this project
  • it's a matter of preference really, but arguably a bad idea

edit:

TLDR II

CONS

  • no distinct advantage
  • can be mistaken by inexperienced as being:
    • jQuery Dependent, or
    • a jQuery Plugin
  • bad organization practices - slaps code onto "jQuery" object
    • like bundling unmatched socks
  • although it's perfectly legal, it's arguably a bad idea

PROS

  • perfectly valid, and was used before jquery
  • short and easily typed
  • makes some feel hip, like ke$ha

PLEASE add to list if you've got suggestions, or troll me if you disagree

Upvotes: 2

Related Questions