- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › jQuery Variables or When should I use a $
I am just getting into jQuery and I know absolutely nothing about javascripting. The one thing I do not understand is the use of the "$". For example how is
var newHeight = $("#page-wrap").height();
different from
var $newHeight = $("#page-wrap").height();
likewise, with the plugin template http://css-tricks.com/snippets/jquery/jquery-plugin-template/, what is the difference between
base.$el = $(el);
and
base.el = el;
Thanks
BZ
The $ is purely optional and doesn’t affect the rest of the code. So why is it sometimes used to ask? Because it helps you to understand what you’re targeting when you use it later. If it’s targeting a DOM property, you don’t use it. When you’re targeting a jquery property, you do. It’s just better for visualization when you’re looking at the code as well as during the coding process. However, feel free to call it whatever you feel comfortable with.
Yea vars don’t need it in jQuery but it is a good way to show they are vars compared to other things… some people like to use other chars like "_" for example.
Thanks, guys. Your answers have been very helpful. That clears up most of my confusion about jQuery. The screencast mentioned above was also a great source of basic syntax and application. I would also recommend the 15 part tutorial available from themeforest.
BZ
The reason I do that on some variable and not others is to designate which ones represent jQuery objects and which ones don’t.
var height = 50;
var width = $(“#thing).width();
It’s totally optional and has no real meaning, just a "convention" I like.