views:

452

answers:

3

I was wondering why in so many jquery plugins $(this) is set to be pointing at $this, here is an example, if i have the following two plugins included on a page:

(function($) {
   jQuery.fn.pluginOne = function() {
      return this.each(function() {
         $this = $(this); <--
         alert($this);
      });
   };
})(jQuery)

(function($) {
   jQuery.fn.pluginTwo = function() {
      return this.each(function() {
         $this = $(this); <--
         alert($this);
      });
   };
})(jQuery)

When i call both plugins on dom ready:

$(document).ready({
   $('.myClass').pluginOne();
   $('.myOtherClass').pluginTwo();
});

The first plugin will get $this from the second plugin... while it i point $(this) to a local var:

(function($) {
   jQuery.fn.pluginTwo = function() {
      return this.each(function() {
         var trigger = $(this); <--
         alert(trigger);
      });
   };
})(jQuery)

everything works at it should, of course...

So my question is... when should I use $this?

Thanks

+7  A: 

$this is a standard people use to cache the actual jQuery object $(this) in a variable.

You can call it what you wish, some people like to refer to it as $self.

e.g

var $self = $(this)

It just helps when you are looking at the code to recognize that it is a jQuery object rather than a normal dom object.

It performs better as you only create one instance of the jQuery variable.

redsquare
The point would be that it should be 'var $this' in the above example, then?
ijw
$whatever is the point
redsquare
...of your answer, yes. Looks to me like the original question related to $this scoping - and, indeed, that the poster is correct (that a variable should be local) and just happens to have two plugins that (a) use $this as a convention, not because it has inherent meaning and (b) don't scope it properly.
ijw
+2  A: 

if you are performing a number of different commands with an element wrapped in a jQuery object, then it is more performant to get the element (as a wrapped set) only once and store that in a local variable

var $this = $(this);
// then use $this in your function.

Note the use of var to locally scope $this

Russ Cam
+3  A: 

$ is a legitimate character for variable and function names in Javascript. That might be confusing if you're coming from PHP where $ is used to signify a variable only (took me a while to realise).

  • In jQuery, $ is an alias for the function jQuery, which is the main function that handles DOM selection. $("#header ul.menu") is the same as jQuery("#header ul.menu"). $ is simply used for brevity.

  • this is a special term in Javascript (not jQ) meaning the current element (in forms you might see this.submit() for example).

  • $(this) means "call the jQuery function with Javascript's this".

  • $this is simply a variable, since $ is a valid character. You can set $this to any value you like and it's completely unrelated to Javascript's this. In general I'd suggest NOT using $ for variable names to avoid confusion with the jQuery function. However, it may be useful to use $var to denote jQuery objects as opposed to normal Javascript variables.

DisgruntledGoat
I actually DO suggest using $ as the first character of a variable when using jQuery. I use it all the time to denote the fact that a certain variable is a jQuery object, such as $this = $(this).
EndangeredMassa
Good point, EM. I edited my answer.
DisgruntledGoat
@EM That's a *fine* idea, I think I might start on that.
ijw