![]() ![]() |
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
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.
thanks ~ Vivre |
![]() ![]() |
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!) |
![]() ![]() |
I think that is because variables in php are preceded by $ |
![]() ![]() |
Hmm, afaik $ is not a reserved character in JavaScript; that's why libraries such as jQuery can use it to name their core function. |
![]() ![]() |
Thanks for your replies :-) Both of these scripts don't use jQuery and look fully selfcontained.
Scribd downloader - http://userscripts.org/scripts/review/116073
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. |
![]() ![]() |
I have to laugh at this line of code from 116073:
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. |
![]() ![]() |
I'm glad to hear you had fun on the way :-)
ok - if those PHP-style variable names aren't unusual, do you know if they have a deeper meaning?
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? ;-) |
![]() ![]() |
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!
|
![]() ![]() |
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 ~ |
![]() ![]() |
You may also want to read Douglas Crockfords advice on JavaScript naming conventions. |
![]() ![]() |
@Bgmin0t ~ thanks for sharing :-)
and half down the page I had a good laugh where it comes to read: "Do not use $ (dollar sign) or \ (backslash) in names." |



~ your help is much appreciated ~ Vivre