Javascript "$" meaning before a variable or namespace - javascript

Anybody knows what is the meaning of the $ sign before an object attribute.
For example:
pages.$page
I've seen in some javascript codes and I'm not sure if it using some kind of framework.
I've used something like this (this.$el) to access to cached element in Backbone.
Thanks in advance!

As far as javascript is concerned, $ is just another character at the beginning of a variable name, at the end of a variable name, in the middle or all by itself.
As far as some frameworks are concerned like jQuery, it's a character that the framework uses in a specific way (by convention, not because it means anything special to javascript).
For example, in jQuery $(selector) is one of the main functions in jQuery and as such it is a popular convention to assign the resulting jQuery object to a variable with a $ in it like this:
var resetButton$ = $("#reset");
This convention then indicates to the reader that the variable contains a jQuery object.
Other frameworks also use the $ sign, some in similar ways, some in other ways, but in all cases, it's just another character in javascript, but because it stands out, it's often used as a meaningful convention.
Once you become familiar with one of these types of conventions, it can make code a lot easier to read and your brain can actually recognize the meaning of the code even quicker with common, learned conventions. But, these conventions of naming variables a certain way are completely optional.

It is used to mark an element or object as a jQuery object (usually). It's a perfectly valid variable name though.

Many people use $varName to indicate that it is a jQuery variable/property
var $divs = $('div');
var nonJqueryVar = 'hello';

Related

What is the primary purpose of function expression in Module Pattern

I know that Module Pattern is very useful and powerful in Javascript programming.
I recognized that pattern in Eric Miraglia's blog for the first time , but I am wondering one thing.
In other blogs and articles that explain Module Pattern, I notice that their sample codes are slightly different from Eric's article, that is, they use function expression with parenthesis rather than function statement, for example, the article from ben cherry is one of them.
Is there any specific reason to use function expression rather than function statement?
Please explain with easy way, I just entered into Javascript Programming world :)
The Miraglia pattern is the same, defining an anonymous function and executing it. The difference is that in order to use the features of the module, you must have a reference to an instance somewhere. Assigning the module to a global variable (YAHOO.*) is a way to retain the reference at a globally known spot, especially important for frameworks (like YUI).
Sometimes you don't need that reference. For example, if you are writing JavaScript for a web page, you often bind events to functions using selectors (ids / types, etc.) That really removes the need for any global reference to your module function.
Hope that make sense...

What does a $ in a javascript function name mean?

GWT outputs codes like this:
function com_mycompany_mywebapp_client_CTest_$$init__Lcom_mycompany_mywebapp_client_CTest_2V(){
this$static}
What does the $ mean, or is it just another character like an underscore? Would this_static mean the same thing?
Nuthin. It's just a character. Think of it as an S with a stick stuck through it.
The dollar sign was added to be used for machine-generated symbols so they wouldn't collide with manually written symbols. But syntactically it's just like underscore, it doesn't mean anything special.
When people discovered they could use just $() as a function name, they started to use it as a shortcut for document.getElementById() and different people extended it in different directions. Now it is often used in libraries and frameworks like Prototype or jQuery which both use $() functions in their own way (that's why you have to use jQuery.noConflict() to use both Prototype and jQuery in the same page). The dollar sign is often used in front of variable names to remember that it's a jQuery object, like this:
var $links = $('a');
But some people don't like it being used that way - eg. see the Code Conventions for the JavaScript Programming Language by Douglas Crockford.

What is the official correct way to declare jQuery variables?

I am trying to learn jQuery. I keep seeing people using different methods of declaring variables.
I have seen var variable;, var $variable;, and $variable;. Which one (if any of these) is the official correct way to declare jQuery variables.
You should always declare a variable with var (whatever the scope, unless creating it on an object), implied globals are bad, don't do it.
As for variable vs. $variable, there is no official stance, just have a convention that you and your team stick to.
As a general rule some follow, (I don't follow this, some do), use variable for variables that aren't jQuery objects and $variable for variables that are, those who use this typically find that seeing $something and knowing immediately it's a jQuery object to be handy.
Use var to define variables to ensure that they are scoped properly, usually locally within a function. My convention is to use $variable when the variable holds a jQuery object (result of a selector) and variable for everything else.
BTW -- they aren't jquery variables, but rather javascript variables. JQuery is just another (albeit the most popular at present) javascript framework, not a language in and of itself.
There is no official correct way to declare variables for jQuery..
They are all javascript variables, and these are the rules for javascript variables
Using the $ at the beginning is a convention that the variable is holding a jQuery object (since the jquery uses the $ sign for the function wrapper)
The diffeernce of using var or not, is in the scope of the variable. Using var declares it local, not using var adds it to the global scope.
Nick already explained why you should use var.
The reason behind variable and $variable is that $ is associated with jQuery and should imply that this variable holds a jQuery object. To avoid unnecessary function calls to jQuery like $(variable) where variable actually already holds a jQuery object.
There is no such thing as a "jQuery variable", they are Javascript variables. When learning jQuery you have to learn some Javascript, as jQuery is just a library for Javascript.
You declare a variable using the var keyword. You can also use a variable without declaring it, and it will implicitly be declared as a global variable, but that is not recommended. You should declare variables in the scope where they are used.
Sometimes putting a $ sign as the first character of a variable name is used to indicate that it's intended for containing a reference to a jQuery object, however there is no official recommendation, and the language itself doesn't care what you name your variables.
The ones with var are local variables, the one without it is global. So it's not just a matter of style. Apart from that use anything you like, personally I haven't seen "$" prefixed variables, but maybe they are useful if you mix a lot of jQuery and standard JavaScript code.

Javascript: $$ Global Variable Considered Evil?

I am writing a lot of Javascript code these days and I am making good use of JQuery. I am wondering if it is considered evil to create strange global variable names. I know that a lot of Javascript frameworks such as JQuery use the dollar character; $, but that greatly simplifies code as it can do so many things.
I am thinking of creating a $$ global variable in my code which would be defined as below:
function $$(tagName)
{
return $('<' + tagName + ' />');
}
The benefit of this is that my code has (1) abstracted out the logic of creating a new element, (2) made the code more concise, and lastly (3) I can almost create html elements within Javascript as concisely as html itself because JQuery has so many other selectors to chain off:
$$('div').attr( { id : 'myDiv', 'class' : 'MyDivClass' }).append(
$$('ul').append(
$$('li').text('first'),
$$('li').text('second'),
$$('li').text('third')
);
);
Do you believe the above approach of creating $$ is legitimate or would you regard it as a no-no?
This is of course an opinion, but if I was new to your application, I would have to go look up what that function does in order to understand the code. Also, the function is simple enough that other developers would likely not bother using it, so you'd end up with a mix of techniques which is confusing.. And really, is this so bad?
$('<div/>').attr( { id : 'myDiv', 'class' : 'MyDivClass' }).append(
$('<ul/>').append(
$('<li/>').text('first'),
$('<li/>').text('second'),
$('<li/>').text('third')
)
);
I prefer a clear, meaningful, self-documenting name over JQuery style $ any day of the year.
Also I find it confusing that the same name has different meanings depending on the context it is used in. Like $ in JQuery or this in javascript and will avoid using them as much as I can.
EDIT: In my opinion it should be up to the user to decide whether he wants to use a shorthand name for the framework he uses. It requires only one line of code:
var $ = jQuery;
When I look at pages with jQuery in it, all I see is a big mess flooded with dollar signs that do different things in different places. If the library had used meaningful names for the functions it offers it would be a lot easier to decipher other peoples code. Javascript is already difficult and unreadable as a language itself.
Personally I don't think it's adding that much functionality to account for the "namespace pollution", not to mention the possible confusion of people reading your code that aren't familiar with your method.
It's just saving a trivial couple of characters on each invocation of the method, so in that respect it can only be considered a form of syntactic sugar. And the cons outweigh the pros, in my opinion.
It would be good if you take a look at the construction of e.g. jQuery .
It does'nt work with several global variables/functions, there is only one global object required "jQuery" .
Everything else are properties of this one object, so they have minified naming-conflicts.
In the special case of $$ : maybe sometimes you need to work with prototypejs, but you cant, because prototypejs already uses $$.
So my suggestion: create one global object, your own "library" , give it a distinct name, and let your functions/variables be member of this object.
I don't think it's "evil" in general, but if you're going to pop something into the global namespace with a "distinguished" name it might be better if it were something a little more interesting. For your purposes, given your example code, the jQuery micro-template system would probably be a better approach anyway.

Why do I see JavaScript variables prefixed with $?

This is sort of a meta-question. Many snippets of JavaScript I've seen here on SO are named with a dollar sign prefix (for example, $id on the second line of the snippet shown in this question). I'm not referring to jQuery or other libraries. I am well aware that this is valid, but it seems awkward to do when not necessary. Why do people name their variables like this? Is it just familiarity with a server-side language like PHP carrying over into their JavaScript code?
I thought perhaps it was to identify a variable as being a jQuery object, for example when you save the result of a selection to a variable in order to eliminate duplicate selections later on, but I haven't seen any consistent convention.
Syntactically, the dollar sign itself means nothing -- to the interpreter, it's just another character, like _ or q. But a lot of people using jQuery and other similar frameworks will prefix variables that contain a jQuery object with a $ so that they are easily identified, and thus not mixed up with things like integers or strings. You could just as easily adopt the same convention by prefixing such variables with jq_ and it would have the same effect.
In effect, it is a crude sort of Hungarian notation.
I will sometimes prefix a variable name with $ to indicate that it is a jQuery-wrapped element (most often when I'm using $(this) in a function a lot, I will assign that to $this).
Part of where the lack of convention may come from is people copy-pasting code together that uses different conventions, thus producing inconsistent code.
Also, sometimes (rarely I hope) people who program in PHP a lot will put $'s at the beginning of their variable names out of habit.
I suspect the person in that example was just copying the jQuery pattern, or is used to PHP/Perl, without really understanding that it isn't necessary and has no special meaning.
However, I have seen [experienced] programmers use it for variable names that are reserved keywords, such as $class or $this. Or even for globals. It's really a personal preference more than anything, in that case.

Categories