text: javascript - What is the reason for var $this = this - Stack Overflow
  I'm not the best at jquery and I came across a var initialization that I don't know why the person who wrote the code did it this way.
  In the init for a plugin, we have
  this.init = function(settings) {
  firstSlide: true,
  ... more code, some uses $this, some uses "this"
  So what is the difference here between "$this" and "this" and why not use one or the other all the time?
  javascript jquery variables this share|improve this question edited Aug 29 '11 at 18:34
  yes it does, it is a jquery idiom
  @Matt: There is no jQuery in the code. Assigning this to another variable is also very common in "plain JavaScript".
  @Felix: It is a plugin, which means that this refers to a jquery object. There is (or used to be) a jquery idiom to prefix jquery objects with $. hence, its a jquery question. If it were non jquery, instead of $this you would probably see, that, self, or something more descriptive as to what this actually means
  @Matt: Oh... In the init for a plugin.... I missed that part. NVM.
  share|improve this answer edited Jan 30 '12 at 21:48
  for older version of jQuery how do you write $this ? jQuerythis??
  var $this is just as any other variable name, the $ and this is just a way to keep another person who reads the code to know what the variable does further down the page. In short terms, yes you could write var jQuerythis but that doesnt really makes any difference. You can use $ in older jQuery aswell, as stated above the $this isnt jQuery dependent, it's just a way to create a copy of this.
  – Lucas Reppe Welander
  In this case, nothing. $this is just another variable declaration which has this assigned to it.
  Typically, I've seen this shortcut used by people using JavaScript libraries when wrapping this. For example, typical usage in jQuery would be:
  // rather than writing $(this) everywhere
  It means nothing in this case (no pun intended). It would be more logical if the statement was var $this = $(this) since that would allow for all the nice jQuery functionality to be used.
  share|improve this answer answered Aug 29 '11 at 18:36
  Incorrect, reassigning this to a different variable allows inner closures to reference the outer scope.
  What everyone else said, also there is an idiom in jquery code to prefix jquery objects with $. don't know how popular it is any more, but used to see a lot of it.
  I do $this = $(this) because it seems that it would save the actual processing of that call every single time you want to use it.
  Also, for the 'magic 'this'' function that someone else mentioned. It is handy to keep the original copy around.
  share|improve this answer answered Jun 18 '13 at 23:25
  In actuality jQuery is a wrapper around the JavaScript DOM, both enhancing and simplifying it. Very briefly JQuery selectors return JQuery Object/s i.e.
  var jQueryResults = $("article"); //Contains all article elements in the DOM as JQuery objects
  However, selecting elements with Javascript returns HTML DOM elements i.e.
  var normalResults = document.getElementsByTagName("article");//Contains all article elements in the DOM as HTML objects
  The issues arise in that DOM objects do not provide the same functionality that JQuery objects provide.
  Here is an event example which illustrates the difference: $('.changeColorHover').hover(function() {
  this.attr("style", "color:red");
  }); //Will not work as we are trying to call a JQuery method on a DOM object
  With the above mentioned in mind the 'this' keyword is a DOM object and thus you are required to convert it to a jQuery object in order to utilise jQuery methods.  $('.changeColorHover').hover(function() {
  }); //Will work since we have first converted the DOM object to a JQuery object
  To sum up, the this keyword, allows you to access the object that called the event as this will give a reference to the object that raised the event. But, this is a DOM object, not a jQuery object. As a result, any of the jQuery methods you'd like to use aren't available, unless you convert it to a jQuery object.
  share|improve this answer answered Jul 18 at 10:53
  Not the answer you're looking for?	Browse other questions tagged javascript jquery variables this or ask your own question.	asked
  4221Javascript function declaration syntax: var fn = function() {} vs function fn() {}1185What is the scope of variables in JavaScript?1121"let" keyword vs "var" keyword54What is the reason behind JSLint saying there are "too many var statements"4777What does "use strict" do in JavaScript, and what is the reasoning behind it?1106What is the purpose of the var keyword and when to use it (or omit it)?230What does 'var that = this;' mean in JavaScript?4What is the reasoning behind declaring var that = this; in javascript?3change "var" inside constructor closure where new instance is created-1Javascript Variable Declaration: this versus var