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.

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • *$* 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 '14 at 06:33
  • It's just a simple global namespace, using a well-known very short identifier? – Bergi Dec 16 '14 at 06:33
  • 1
    Explained [here](http://stackoverflow.com/questions/7541218/writing-jquery-static-util-methods/7541692#7541692) and [here](http://stackoverflow.com/questions/25493134/why-do-people-add-their-own-custom-user-functions-to-the-jquery-object/25494140#25494140). Note sure if this is a pure dup of either of those, but I think you'll find things explained there. – jfriend00 Dec 16 '14 at 06:34
  • $ is not a prerogative to jquery. its just a namespace identifier. – Prabhu Murthy Dec 16 '14 at 06:36
  • @jfriend00—"*None of these modules seems to be intended as a jQuery plugin*…". I read that as meaning "jQuery isn't being used". And if it is, I don't think it's a good idea to add them as properties of the jQuery object, better to use a different "namespace" object to clearly distinguish the two. – RobG Dec 16 '14 at 06:38
  • possible duplicate of [What is the $ symbol used for in JavaScript](http://stackoverflow.com/a/3107561/1048572) – Bergi Dec 16 '14 at 06:41
  • @RobG - It's just a namespace discussion whether jQuery exists or not and the impact is how many top level items you're going to add to the namespace. I didn't mark it a dup because he's asking it slightly different. – jfriend00 Dec 16 '14 at 06:41
  • 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 '14 at 06:45
  • @ProfK - Since jQuery is present then it's just a designer's choice for whether they want to create a new top level namespace object or use the jQuery one that already exists. The answers I pointed to in an earlier comment explain the tradeoffs between those two choices. There is no perfectly right answer, it's just a choice to be made. – jfriend00 Dec 16 '14 at 06:54

1 Answers1

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

Community
  • 1
  • 1
Todd
  • 5,314
  • 3
  • 28
  • 45