JavaScript Double Dollar Sign

I understand the use of the (single) dollar sign in popular JavaScript libraries such as jQuery and Prototype. I also understand the meaning of the double dollar sign in PHP (variable variables). Dean Edwards uses the double dollar sign in his famous JavaScript addEvent () function. Except that the double dollar sign is used:

function addEvent(element, type, handler) { // assign each event handler a unique ID if (!handler.$$guid) handler.$$guid = addEvent.guid++; // create a hash table of event types for the element if (!element.events) element.events = {}; // create a hash table of event handlers for each element/event pair var handlers = element.events[type]; if (!handlers) { handlers = element.events[type] = {}; // store the existing event handler (if there is one) if (element["on" + type]) { handlers[0] = element["on" + type]; } } // store the event handler in the hash table handlers[handler.$$guid] = handler; // assign a global event handler to do all the work element["on" + type] = handleEvent; }; // a counter used to create unique IDs addEvent.guid = 1; 

From what I read, the dollar sign only officially matters in JavaScript for code generation tools, although this standard is largely ignored today due to the widespread use of dollar signs in the above JavaScript libraries.

Can anyone shed some light on this use of the double dollar sign in JavaScript?

Thank you very much!

+37
javascript
Sep 23 '09 at 3:57
source share
5 answers

I wrote this function. :)

$$ prefixes no longer have a special meaning. This was the notation I used when the packer was popular. The packer abbreviated variable names with this prefix. Currently, Packer automatically abbreviates variable names, so this designation is redundant.

Short answer, the $ sign is a valid identifier in JavaScript, so you are just looking at a bunch of ugly variable names .;)

+74
Sep 24 '09 at 0:27
source share

Well, this is just a developer naming convention. " $$ " can also be " aa ", for all purposes and tasks.

As you already mentioned, one signed $() dollar sign function has become almost standard in various JavaScript libraries. "One function to rule them all," huh? But remember that this is not inherent in JS itself. This is simply the name of the function that replaced document.getElementById() .

So, with that said, $$ can be anything. Damn, you might have a function like this:

 function $$$$$$$$(){ //i've got dollar signs in my eyes! } 

In other words, it's just the name of a variable / function - like everyone else.

+20
Sep 23 '09 at 4:09
source share

Like others, $$ is an arbitrary identifier.

MochiKit's JavaScript toolkit retains a function identified by the same method as selecting elements using CSS selector syntax like jQuery, except that it does not wrap all relevant elements in a special object.

So yes, this is nothing special, in and of itself.

+6
Sep 23 '09 at 4:26
source share

As far as I know, a dollar sign is an ordinary character that can be used in variable names. This may be a convention, meaning a property that should not be mixed.

I assume that the author used $$guid to avoid clashing with existing property names. He could probably use __guid or something like that.

+3
Sep 23 '09 at 4:05
source share

The double dollar sign refers to the following terms -

 //getElementById function $$(id){ return document.getElementById(id); } 
+1
Sep 19 '13 at 10:43 on
source share



All Articles