Why do people assign $ this = $ (this) in many jQuery plugins?

I often see this as the first line of the plug-in:

$this = $(this); 

Is it just for efficiency to avoid getting a jQuery object every time?

+7
source share
2 answers

To cache a jQuery object and not create it every time it requires it.

+6
source

As in previous answers, it will cache the object - kind of.

If you call $(this) , jQuery will search the DOM until it finds this -element. If you want to make many changes to an element, it will be faster to save a link to this -element.

 $this = $(this); 

Now the element is saved as the $this variable, and if you want to add material to it again, you simply use the variable.

 $this.hide(); //hides the element. 
+2
source

All Articles