1

i have a simple question about caching, this kind of confuses me lol Im not sure if it depends on if your using handlers or not but...

is there any difference between these two?

var $something = $('.something');

or

var something = $('.something');

and also, is it possible to do this (depending on the correct way)

var something = $('something'),
    somethingElse = $('somethingelse');

or this way

var something = $('something');
var somethingElse = $('somethingelse');

just want to be sure im heading in the right direction.

It's been bothering me. I've seen it done both ways actually, but i don't know which is right, or if either are wrong. I'm sure someone knows for sure though :)

Nick Rameau
  • 1,258
  • 14
  • 23
Sin
  • 265
  • 2
  • 7
  • 20
  • [jquery variable syntax](http://stackoverflow.com/questions/1916584/jquery-variable-syntax), [javascript declare multiple variables](http://stackoverflow.com/questions/4082560/javascript-declare-multiple-variables) – ifaour Dec 19 '10 at 13:01

2 Answers2

2

Prefixing variables with $ is only used to remind the programmer (or others) that the variable holds a jquery object. It isn't a 'javascript thing' and does not provide any additional functionality. It's a good idea though :)

All the code you posted is valid.

sje397
  • 41,293
  • 8
  • 87
  • 103
  • Cool, thanks a lot :) Its always cool to start out with the correct ways of doing something – Sin Dec 19 '10 at 12:55
1

It's your choice. Many people (including myself) prefix variables with $ to indicate that the variables represent jQuery objects (because $ is shorthand for jQuery). If you think it helps you along the same lines, then you're free to prefix your variables as such.

Declaring multiple variables comma-separated with a single var keyword is legal JavaScript.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    The funny thing (to me) is that there are people who do this, but don't use prefixes for other things in the old tradition of [Hungarian notation](http://en.wikipedia.org/wiki/Hungarian_notation). Just seems an odd dichotomy. And yet, I'm probably one of them. If I have a variable called `form`, it points to a DOM element. If it's called `$form`, it points to a jQuery wrapper around one. And yet I don't use `sName` for name strings... :-) – T.J. Crowder Dec 19 '10 at 12:34
  • I've noticed working with wordpress, they prefer you use the jQuery instead of the $. does this mean I change every $? or just the one before the function? – Sin Dec 19 '10 at 12:59
  • yeah but that's because people (read myself) have a hard time remembering if the variable/DOM element is already jQueried, but people know what PODs are – qwertymk Dec 19 '10 at 12:59
  • @Sin: If you will only use jQuery and not other JS libraries it's fine to use `$`. – BoltClock Dec 19 '10 at 13:01
  • @BoltClock Ah i see...now it's all clear to me. Cool, thanks for the insight. My next obstacle is conquering the $get() with ajax....that's gonna be fun times :) – Sin Dec 19 '10 at 13:11
  • @Sin: Be sure to mark answers to your questions as accepted by clicking the checkmark. Welcome to Stack Overflow! – BoltClock Dec 19 '10 at 13:14