what is special about '$'

in Script development
Subscribe to what is special about '$' 11 posts, 6 voices



Vivre Scriptwright
FirefoxX11

I would like to find out what is the special usage of '$' in js.

There are at least two scripts I would use but they won't work for me and it seems it's because the author uses '$' as variable or proceeding part of a textual variable
e.g.:
var $ = function xy
var $$ = function xy
var $abc = "something"

I know '$' as particular replace-placeholder in regex but when and why is it used in js and why not any other 'fancy' character.

I'm not asking for a comprehensive answer (though I wouldn't neglect it ;-) ) but what is this 'characters'-function/usage called/named so I could have a chance to find sth. about it on the net or on js-sides.
Or could someone please leave me a link to a page concering this matter to have a starting point.

thanks ~ Vivre

 
Jefferson Scher Scriptwright
FirefoxWindows

My first guess is that the script requires the jQuery library, which uses $ as some kind of a magical element selector. (I have a book on jQuery, but didn't read that much of it, obviously!)

 
w35l3y Scriptwright
FirefoxWindows

I think that is because variables in php are preceded by $

 
SBscripts Scriptwright
FirefoxWindows

Hmm, afaik $ is not a reserved character in JavaScript; that's why libraries such as jQuery can use it to name their core function.

 
Vivre Scriptwright
FirefoxX11

Thanks for your replies :-)

Both of these scripts don't use jQuery and look fully selfcontained.
I leave the links in case you'd have a look:

Scribd downloader - http://userscripts.org/scripts/review/116073
USO Quick Home - http://userscripts.org/scripts/review/132152

In the first one this $ is used so often, (even within one closed expression/function) that I can hardly make out when it is meant as 'private' variable or regex-placeholder and if they couldn't accidently interfere with one another.

The second one is less complex.

Maybe the authors come from php and just use it as they're used to do it?

But do you understand why this character is used at all? Does it contain a certain information that can't be carried within an ordinary variable. Could they be replaced by 'plain words'? What is the need or advantage to use this kind of syntax?

And last but not least - how could I exchange them or compensate their functionality so that I could include my own workarounds whenever this syntax crosses my way.

Maybe it's even only due to 'unproper' declaration or unlucky order. But as long as this $-specific is so obscure to me I don't know where to start.

 
Jefferson Scher Scriptwright
FirefoxWindows

I have to laugh at this line of code from 116073:

return $$?$$[0]:$$;

I prefer to write code that is more self-documenting. In other words, I have no idea what is going on in this script.

In 132152 I just see PHP-style variable names. Nothing unusual.

 
Vivre Scriptwright
FirefoxX11

I'm glad to hear you had fun on the way :-)
and besides eases me as well

ok - if those PHP-style variable names aren't unusual, do you know if they have a deeper meaning?
Is when used in js e.g. $word === word ? Is it just a question of taste which style I use?

Well - I removed all $ from the second script and it won't work none the less so unfortunately this doesn't prove a thing.

There must be a reason / advantage / necessity / whatever to take the burden of having to extra-click shift all the time just to apply a useless character? Or is it after all an unspoken secret of adepts? ;-)

 
Watilin Scriptwright
FirefoxWindows

Hi again Vivre,

as I explained in our precedent conversation, I use $ in no other purpose than marking variables of type HTML elements. The $ is a generic character in JavaScript and one can use it in any variable name, at any position.

About the "Scribd Downloader" script... I wouldn't want to be maintainer of that!
I've guessed that the $$ is a shortcut for document.getElementsByClassName (or a placeholder when the browser doesn't support it), while $ takes role for several local variables.

 
Vivre Scriptwright
FirefoxX11

Hi Watilin - dito again 2 ;-)

great - that gives me a better understanding, a basis and several aspects to search for and regard from now on.

thanks @all ~ ~ your help is much appreciated ~ Vivre

 
SUPER_KEG Scriptwright
ChromeWindows

You may also want to read Douglas Crockfords advice on JavaScript naming conventions.

 
Vivre Scriptwright
FirefoxX11

@Bgmin0t ~ thanks for sharing :-)
- that's a valuable hint collection (& keeper)

and half down the page I had a good laugh where it comes to read:

"Do not use $ (dollar sign) or \ (backslash) in names."