Why do people assign $this = $(this) in many jQuery plugins?
I often see this as the first line of a plug-in:
$this = $(this);
Is this just for efficiency, to avoid getting the jQuery object each and every t开发者_如何学JAVAime?
To cache the jQuery object and not have to instantiate it every time they require it.
Just as the above answers says it will cache the object - sort of.
If you call $(this)
jQuery will search in the DOM until it finds this
-element. If you want to do alot of changes to the element, it will be faster to save the reference to this
-element.
$this = $(this);
Now the element is saved as the $this
variabel and if you want to do stuff to it again, you just use the variabel.
$this.hide(); //hides the element.
精彩评论