Rusty Rob
Rusty Rob

Reputation: 17193

why type $this = $(this) in jquery

I'm new to jQuery and have noticed someone go:

var $this = $(this);

Why do this? Is it to save typing? Does it help performance? Is it fairly standard practice?

Also I've started doing things such as:

var minus_button = $('#minus_button');

Should this instead be var $minus_button = $('#minus_button'); to signal it's a jquery object?

I read http://docs.jquery.com/JQuery_Core_Style_Guidelines but couldn't find any suggestions.

Upvotes: 2

Views: 253

Answers (2)

user99874
user99874

Reputation:

It's just a form of Hungarian Notation.

Upvotes: 1

Gabe
Gabe

Reputation: 50503

Yes, it's a naming convention to signal that that variable is a jquery object reference. That way it is very obvious whether or not you can use a jquery function on the object or if the object needs to be converted into a jquery object to apply said function.

Example:

var element = document.getElementById('myelement');
var $element = $('#myelement');

// not a jquery object
console.log($(element).val());
// jquery object
console.log($element.val());

Upvotes: 6

Related Questions