2

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!

JuanO
  • 2,500
  • 3
  • 18
  • 20
  • `$` within variable name is quite valid, so maybe `this` contains property names `$el` – Jovan Perovic Apr 25 '12 at 19:31
  • 1
    possible duplicate of [Why would a JavaScript variable start with a dollar sign?](http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign) – Quentin Apr 25 '12 at 19:33

3 Answers3

8

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
2

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

Joe
  • 80,724
  • 18
  • 127
  • 145
1

Many people use $varName to indicate that it is a jQuery variable/property

var $divs = $('div');
var nonJqueryVar = 'hello';
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Thx Juan, I think that it could be a good use to highlight the difference. In this code they use jquery templates, so could be a good name convention. Thkx All ! – JuanO Apr 25 '12 at 19:35