What is the difference between these three forms:
这三种形式有什么区别:
this
$this
$(this)
6 个解决方案
#1
48
In typical usage you'll usually see them like this (the $this
usage may vary):
在典型的使用中,你通常会看到它们(这个用量可能会有所不同):
-
this
- Refers to the DOM element in the handler you're currently on, but this may be another object entirely in other situations, but it's always the context. - this - 指您当前处理的处理程序中的DOM元素,但这可能是完全在其他情况下的另一个对象,但它始终是上下文。
-
$this
- Usually created byvar $this = $(this)
a cached version of the jQuery wrapped version for efficiency (or chain off$(this)
to get the same in many cases). - $ this - 通常由var $ this = $(this)创建jQuery包装版本的缓存版本以提高效率(或链接$(this)以在许多情况下获得相同的效果)。
-
$(this)
- The jQuery wrapped version of the element, so you have access to all its methods (the ones in$.fn
specifically). - $(this) - jQuery包装的元素版本,因此您可以访问其所有方法(特别是$ .fn中的方法)。
#2
31
-
this
is the object upon which a method was called - 这是调用方法的对象
-
$this
is a poorly named variable with no special meaning - $这是一个命名不佳的变量,没有特殊含义
-
$(this)
calls the poorly named function$
withthis
as its only argument - $(this)使用this作为唯一参数调用命名不佳的函数$
#3
9
In jQuery event handler:
在jQuery事件处理程序中:
-
this
- is a DOM element you assigned the event handler to - this - 是您为其分配事件处理程序的DOM元素
-
$(this)
- is a jQuery object created from that element - $(this) - 是从该元素创建的jQuery对象
-
$this
- typically, a variable holding the result of$(this)
- $ this - 通常是一个保存$(this)结果的变量
More generally:
更普遍:
-
this inside a function refers to the object or primitive the function is called on. When a function is used as a constructor, it refers to the new object being constructed. Outside of any function
this
refers to the global object (window
in non-strict mode).函数内部的this是指调用函数的对象或原语。当函数用作构造函数时,它引用正在构造的新对象。在任何函数之外,这指的是全局对象(非严格模式下的窗口)。
You can find a good detailed explanation on MDN.
您可以在MDN上找到详细的详细说明。
-
$this is a variable name. In JavaScript variable names can start with
$
. Some like to use it as a prefix for variables containing jQuery objects:$ this是变量名。在JavaScript中,变量名称可以以$开头。有些人喜欢将它用作包含jQuery对象的变量的前缀:
var body = document.body; // no prefix for a plain DOM object var $body = jQuery('body'); // prefix for the same object wrapped in jQuery var $this = $(this);
-
$(this) is a function call, where
$
is a function name, andthis
is its argument:$(this)是一个函数调用,其中$是函数名,这是它的参数:
var $ = alert; $(this); // [object Window]
$
doesn't have any special meaning per se. But jQuery defines the$()
function as a shorthand forjQuery()
. Depending on its arguments, this function can do many different things.$本身没有任何特殊含义。但是jQuery将$()函数定义为jQuery()的简写。根据其参数,此函数可以执行许多不同的操作。
#4
1
In the context of jQuery, 'this' is the object upon which a method was called. '$this' is indeed a poorly named variable with no special meaning. '$(this)' passes 'this' to jQuery, which will return a jQuery object associated with whatever 'this' is, as long as 'this' is a DOM object.
在jQuery的上下文中,'this'是调用方法的对象。 '$ this'确实是一个命名不佳的变量,没有特殊含义。 '$(this)'将'this'传递给jQuery,只要'this'是一个DOM对象,它就会返回与'this'相关的jQuery对象。
#5
0
Your question would be better served with more context.
您可以通过更多背景更好地解决问题。
However I assume you're asking about variables within the context of a callback on an element's event (click
for example).
但是我假设您在元素事件的回调上下文中询问变量(例如,单击)。
-
this
is the context of your handler (normally the DOM element, in the case of a DOM event handler) - 这是处理程序的上下文(通常是DOM元素,在DOM事件处理程序的情况下)
-
$this
is usually used to store the result of$(this)
- $ this通常用于存储$(this)的结果
-
$(this)
returns the jQuery object that wrapsthis
- see the jQuery documentation for more information. - $(this)返回包装它的jQuery对象 - 有关更多信息,请参阅jQuery文档。
#6
0
Expanding on what David said:
扩展大卫所说的话:
-
$this
is usually used to have a copy of thethis
object in the current scope. For example withvar $this = this;
you can use the variable$this
anywhere in the current scope and always be able to reference that object that would otherwise change if simply referenced withthis
... I personally dislike the$this
naming convention and prefer something likevar parentScope
$ this通常用于在当前范围内拥有此对象的副本。例如,var $ this = this;你可以在当前范围内的任何地方使用变量$ this,并且总是能够引用那个如果简单地用这个引用会改变的对象......我个人不喜欢$ this命名约定并喜欢像var parentScope这样的东西
-
$(this)
is a function (var $ = function(){}
) used by some frameworks like jQuery or PrototypeJs. The reason it is used is because$
is very easy to type instead ofsomeLongFunctionName
and because it is usually called many times in the code it's easier to have it be as short as possible$(this)是一些函数(var $ = function(){}),用于某些框架,如jQuery或PrototypeJs。使用它的原因是因为$很容易键入而不是someLongFunctionName,并且因为它通常在代码中多次调用,所以它更容易让它尽可能短
#1
48
In typical usage you'll usually see them like this (the $this
usage may vary):
在典型的使用中,你通常会看到它们(这个用量可能会有所不同):
-
this
- Refers to the DOM element in the handler you're currently on, but this may be another object entirely in other situations, but it's always the context. - this - 指您当前处理的处理程序中的DOM元素,但这可能是完全在其他情况下的另一个对象,但它始终是上下文。
-
$this
- Usually created byvar $this = $(this)
a cached version of the jQuery wrapped version for efficiency (or chain off$(this)
to get the same in many cases). - $ this - 通常由var $ this = $(this)创建jQuery包装版本的缓存版本以提高效率(或链接$(this)以在许多情况下获得相同的效果)。
-
$(this)
- The jQuery wrapped version of the element, so you have access to all its methods (the ones in$.fn
specifically). - $(this) - jQuery包装的元素版本,因此您可以访问其所有方法(特别是$ .fn中的方法)。
#2
31
-
this
is the object upon which a method was called - 这是调用方法的对象
-
$this
is a poorly named variable with no special meaning - $这是一个命名不佳的变量,没有特殊含义
-
$(this)
calls the poorly named function$
withthis
as its only argument - $(this)使用this作为唯一参数调用命名不佳的函数$
#3
9
In jQuery event handler:
在jQuery事件处理程序中:
-
this
- is a DOM element you assigned the event handler to - this - 是您为其分配事件处理程序的DOM元素
-
$(this)
- is a jQuery object created from that element - $(this) - 是从该元素创建的jQuery对象
-
$this
- typically, a variable holding the result of$(this)
- $ this - 通常是一个保存$(this)结果的变量
More generally:
更普遍:
-
this inside a function refers to the object or primitive the function is called on. When a function is used as a constructor, it refers to the new object being constructed. Outside of any function
this
refers to the global object (window
in non-strict mode).函数内部的this是指调用函数的对象或原语。当函数用作构造函数时,它引用正在构造的新对象。在任何函数之外,这指的是全局对象(非严格模式下的窗口)。
You can find a good detailed explanation on MDN.
您可以在MDN上找到详细的详细说明。
-
$this is a variable name. In JavaScript variable names can start with
$
. Some like to use it as a prefix for variables containing jQuery objects:$ this是变量名。在JavaScript中,变量名称可以以$开头。有些人喜欢将它用作包含jQuery对象的变量的前缀:
var body = document.body; // no prefix for a plain DOM object var $body = jQuery('body'); // prefix for the same object wrapped in jQuery var $this = $(this);
-
$(this) is a function call, where
$
is a function name, andthis
is its argument:$(this)是一个函数调用,其中$是函数名,这是它的参数:
var $ = alert; $(this); // [object Window]
$
doesn't have any special meaning per se. But jQuery defines the$()
function as a shorthand forjQuery()
. Depending on its arguments, this function can do many different things.$本身没有任何特殊含义。但是jQuery将$()函数定义为jQuery()的简写。根据其参数,此函数可以执行许多不同的操作。
#4
1
In the context of jQuery, 'this' is the object upon which a method was called. '$this' is indeed a poorly named variable with no special meaning. '$(this)' passes 'this' to jQuery, which will return a jQuery object associated with whatever 'this' is, as long as 'this' is a DOM object.
在jQuery的上下文中,'this'是调用方法的对象。 '$ this'确实是一个命名不佳的变量,没有特殊含义。 '$(this)'将'this'传递给jQuery,只要'this'是一个DOM对象,它就会返回与'this'相关的jQuery对象。
#5
0
Your question would be better served with more context.
您可以通过更多背景更好地解决问题。
However I assume you're asking about variables within the context of a callback on an element's event (click
for example).
但是我假设您在元素事件的回调上下文中询问变量(例如,单击)。
-
this
is the context of your handler (normally the DOM element, in the case of a DOM event handler) - 这是处理程序的上下文(通常是DOM元素,在DOM事件处理程序的情况下)
-
$this
is usually used to store the result of$(this)
- $ this通常用于存储$(this)的结果
-
$(this)
returns the jQuery object that wrapsthis
- see the jQuery documentation for more information. - $(this)返回包装它的jQuery对象 - 有关更多信息,请参阅jQuery文档。
#6
0
Expanding on what David said:
扩展大卫所说的话:
-
$this
is usually used to have a copy of thethis
object in the current scope. For example withvar $this = this;
you can use the variable$this
anywhere in the current scope and always be able to reference that object that would otherwise change if simply referenced withthis
... I personally dislike the$this
naming convention and prefer something likevar parentScope
$ this通常用于在当前范围内拥有此对象的副本。例如,var $ this = this;你可以在当前范围内的任何地方使用变量$ this,并且总是能够引用那个如果简单地用这个引用会改变的对象......我个人不喜欢$ this命名约定并喜欢像var parentScope这样的东西
-
$(this)
is a function (var $ = function(){}
) used by some frameworks like jQuery or PrototypeJs. The reason it is used is because$
is very easy to type instead ofsomeLongFunctionName
and because it is usually called many times in the code it's easier to have it be as short as possible$(this)是一些函数(var $ = function(){}),用于某些框架,如jQuery或PrototypeJs。使用它的原因是因为$很容易键入而不是someLongFunctionName,并且因为它通常在代码中多次调用,所以它更容易让它尽可能短