0
/**
 * @author paula
 */

function A(name){
    this.name = name;
    this.sayHi();
    this.$sayHi();
    this.sayBye();
};

(function(){
    this.sayHi = function(){
        console.log("[sayHi] Hi there, "+ this.name);
    };

    this.$sayHi = function(){
        console.log("[$sayHi] Hi there, " + this.name);
    };

    this.sayBye = function(){
        console.log("[sayBye] Bye bye now, " + this.name);
    };
}).call(A.prototype);

var objA1 = new A("Luca");

Found this article: javascript.crockford.com/code.html where is mentioned that "Do not use $ (dollar sign) or \ (backslash) in names. Most variables and functions should start with a lower case letter"

I have some framework code with many functions prefixed with dollar sign (functions that look similar to $sayHi). Is a function prefixed with $ intended to be used for special purposes ? Thanks.

Paula Cogeanu
  • 231
  • 1
  • 10
  • 7
    The dollar sign is a valid identifier, it makes no difference. – elclanrs Mar 22 '14 at 14:42
  • Dollar sign appears above alphabet characters when function names are sorted alphabetically, and helps separate framework functions from your own functions. – 3urdoch Mar 22 '14 at 14:43
  • hey, it's not a duplicate – Paula Cogeanu Mar 22 '14 at 14:48
  • 1
    Sure looks like a duplicate to me. What distinguishes the two? – cookie monster Mar 22 '14 at 14:51
  • `$` is perhaps the easiest and free non reserved javascript character - this is why jquery use it as their function identifier, basically the shortest thing that was widely available ( annoying to many who used it before jquery became popular : ) – Rob Sedgwick Mar 22 '14 at 15:02
  • No, it's not a duplicate. I'm focused on the function names prefixed with dollar sign and I want to clarify if there is any naming convention regarding that. – Paula Cogeanu Mar 22 '14 at 15:04
  • @PaulaCogeanu: There is not really a convention for function (method) names wit dollar signs, even less than for variable (property) names like in the duplicate question. You can use `$` in any identifier just as you want, if some people want to read any deeper meaning into that it's their business and always depends on the context. – Bergi Mar 22 '14 at 15:08
  • @cookie monster. No. It's not. – Paula Cogeanu Mar 22 '14 at 15:10
  • There's no real distinction. `$` is a valid identifier with no magic behavior. Either way, this question has been asked many, many times. The duplicates will give you the answer you're looking for, so I don't know what you're complaining about – cookie monster Mar 22 '14 at 15:10
  • It has to be something that makes the distinction, why to use it ... just because it's pretty and nice to have a function prefixed with dollar sign ? – Paula Cogeanu Mar 22 '14 at 15:15
  • People can name functions however they want, and if they want to do something as silly as put a `$` before one, that's up to them. But your question wasn't asking about naming conventions. It was asking about differing behavior, and that also is answered by the duplicates. – cookie monster Mar 22 '14 at 15:17
  • *"...to understand if there is some magic power around the function prefixed with dollar sign ($)"* *"...failed to find if $sayHi is treated differently by JS interpreter"* – cookie monster Mar 22 '14 at 15:19
  • I for one haven't seen any frameworks use a `$` prefix for functions, other than a main function/namespace named `$` in libraries like jQuery and PrototypeJS. So I don't understand why you think it's some sort of convention. – cookie monster Mar 22 '14 at 15:22
  • @cookie monster. I'm focused to understand why and when somebody should choose to prefix his functions with dollar sign. Anyway, it looks more interesting to me the murdoch answer. – Paula Cogeanu Mar 22 '14 at 15:34
  • Yeah, I have no idea why someone would do that. Using an underscore prefix to denote "no touchy" properties is fairly common. Haven't seen `$` in the wild for that purpose though. I'd have a tough time with an API that actually used `$` for public methods. – cookie monster Mar 22 '14 at 15:52
  • @PaulaCogeanu, I'm seeing the `$` used to prefix jquery selectors quite a bit. `var $myDiv = $("#myDiv");` and also `$this = $(this)` - i like it, can at a glance see that it refers to a jquery object – Rob Sedgwick Mar 24 '14 at 20:20
  • @Rob Sedgwick In the provided example it can be seen that $sayHi refers to a function not to a jQuery object ( a div element as you suggested ). I'm looking for a naming convention for functions which are prefixed by $. Thanks anyway for your input. – Paula Cogeanu Mar 27 '14 at 10:39
  • @PaulaCogeanu, yeah, I can't see any real reason why folks would prefix with a $, other than when refering to jquery/proto/sizzle objects - must be just a preference of that dev. - saying that if a functon has jquery/proto/sizzle dependancies, it could be could too prefix with $ too. ( i'd like that too )- but, yeah, there are no no rules regarding `$` – Rob Sedgwick Mar 27 '14 at 19:17

3 Answers3

0

You never have to prefix anything with a dollar sign, it's just a popular choice in some libraries like jQuery, because $ is not often used as a normal variable name. The dollar sign is a valid variable name, so it can be used to name any variable.

this.$sayHi=function(){/*code*/};

and

this.sayHi=function(){/*code*/};

are just two different variable names with different things assigned to them. You can name a variable anything you want, but putting a dollar sign in front of it doesn't make it special.

Also, of note, is that in jQuery and similar libraries/frameworks, there will always be a dot after the dollar sign. Basically, the frameworks only want to take up one global variable, and they make it the dollar sign, which nobody really should be using as a variable name, since it's kind of really undescriptive.

scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
  • Thanks for your answer. I understand that in JS is perfectly valid to prefix anything(function names) with dollar sign. But what I would like to know is if there is any message to transmit to other developers when you decide to prefix your function with $, message which refers on how the method should be used (to make them aware that the function is public or private). – Paula Cogeanu Mar 22 '14 at 22:10
0

As per ECMA 5.1 Standard specification, a variable's identifier is defined like this

Identifier ::
    IdentifierName but not ReservedWord

And IdentifierName is defined like this

IdentifierName ::
    IdentifierStart
    IdentifierName IdentifierPart

And IdentifierStart is defined, like this

IdentifierStart ::
    UnicodeLetter
    $
    _ 
    \ UnicodeEscapeSequence

So, $ is nothing but a character in the variable's name.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Notice that it's not a variable, but a proberty - where all `IdentifierName`s are allowed – Bergi Mar 22 '14 at 15:05
  • ok, thank you for your answer. Looks that function names prefixed with more than one dollar sign ($$sayHi or $$$sayHi) are also valid names. The Python convention for making a function/method private is to prefix it with an _ (underscore). $ (dollar sign) in JS has the same meaning as _ (underscore) in Python ? – Paula Cogeanu Mar 22 '14 at 21:48
  • @PaulaCogeanu As you said, its just a convention. In JS also, people use `__` for internal or private variables. – thefourtheye Mar 23 '14 at 03:09
  • In this article http://javascript.crockford.com/code.html it is stated that: " Do not use $ (dollar sign) or \ (backslash) in names. Do not use _ (underbar) as the first character of a name. It is sometimes used to indicate privacy, but it does not actually provide privacy. If privacy is important, use the forms that provide private members. Avoid conventions that demonstrate a lack of competence. Most variables and functions should start with a lower case letter." – Paula Cogeanu Mar 23 '14 at 15:35
  • I have some code where functions which are prefixed with dollar sign return an array and I do not know if it's a new convention or is sth else. – Paula Cogeanu Mar 23 '14 at 15:45
  • @thefourtheye I would like to know what is the convention related to the dollar sign. Found some references saying that a variable prefixed with a $ is a new variable created in global space. I'm looking to clarify this aspect: what is the naming convention for functions prefixed with dollar sign (in my example is $sayHi). Do you have any clue ? Thanks. – Paula Cogeanu Mar 23 '14 at 22:08
  • @PaulaCogeanu `__` is quite commonly used, not sure about `$` though. I would not personally use `$` in a variable name though – thefourtheye Mar 24 '14 at 00:02
0

The dollar sign is like any other letter, as far as javascript is concerned. For instance:

var $ = 5;
console.log($);

Function names can also have dollar signs:

function $() {
   console.log("Hello world!");
}
$();

JQuery (and other libraries) create a function dollar sign to do amazing things. It is a good name for these functions because you are not likely to create a method named $ in your code.

PlasmaPower
  • 1,864
  • 15
  • 18