patternjavascriptCritical
Why would a JavaScript variable start with a dollar sign?
Viewed 0 times
withdollarwhysignstartwouldvariablejavascript
Problem
I quite often see JavaScript with variables that start with a dollar sign. When/why would you choose to prefix a variable in this way?
(I'm not asking about
(I'm not asking about
$('p.foo') syntax that you see in jQuery and others, but normal variables like $name and $order)Solution
Very common use in jQuery is to distinguish jQuery objects stored in variables from other variables.
For example, I would define:
I find this to be very helpful in writing jQuery code and makes it easy to see jQuery objects which have a different set of properties.
For example, I would define:
var $email = $("#email"); // refers to the jQuery object representation of the dom object
var email_field = $("#email").get(0); // refers to the dom object itselfI find this to be very helpful in writing jQuery code and makes it easy to see jQuery objects which have a different set of properties.
Code Snippets
var $email = $("#email"); // refers to the jQuery object representation of the dom object
var email_field = $("#email").get(0); // refers to the dom object itselfContext
Stack Overflow Q#205853, score: 1541
Revisions (0)
No revisions yet.