4

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.

9
  • $ was used as an identifier long before jQuery appeared. Use it if you want. In ECMAScript ed 3, it was intended to be reserved for machine generated code. That restriction was ignored by a number of libraries, and is removed in ES5.
    – RobG
    Dec 16, 2014 at 6:33
  • It's just a simple global namespace, using a well-known very short identifier?
    – Bergi
    Dec 16, 2014 at 6:33
  • 1
    Explained here and here. Note sure if this is a pure dup of either of those, but I think you'll find things explained there.
    – jfriend00
    Dec 16, 2014 at 6:34
  • $ is not a prerogative to jquery. its just a namespace identifier. Dec 16, 2014 at 6:36
  • 1
    @RobG jQuery is being used, and that's what concerns me. just using acme would also be fine as a simple, global namespace.
    – ProfK
    Dec 16, 2014 at 6:45

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.