javascript “this”、“$this”和“$(this)”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3889570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:34:26  来源:igfitidea点击:

What is the difference between “this”, “$this” and “$(this)”?

javascriptjquery

提问by oshirowanen

What is the difference between these three forms:

这三种形式有什么区别:

this
$this
$(this)

回答by Nick Craver

In typicalusage you'll usually see them like this (the $thisusage may vary):

典型用法中,您通常会看到它们是这样的($this用法可能会有所不同):

  • 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- Usually created by var $this = $(this)a cached version of the jQuery wrapped version for efficiency (or chain off $(this)to get the same in many cases).
  • $(this)- The jQuery wrapped version of the element, so you have access to all its methods(the ones in $.fnspecifically).
  • this- 指的是您当前所在的处理程序中的 DOM 元素,但这在其他情况下可能完全是另一个对象,但它始终是上下文。
  • $this- 通常由var $this = $(this)jQuery 包装版本的缓存版本创建以提高效率(或$(this)在许多情况下链接以获得相同的效果)。
  • $(this)- 元素的 jQuery 包装版本,因此您可以访问其所有方法(特别是那些方法$.fn)。

回答by Quentin

  • thisis the object upon which a method was called
  • $thisis a poorly named variable with no special meaning
  • $(this)calls the poorly named function $with thisas its only argument
  • this是调用方法的对象
  • $this是一个命名不当的变量,没有特殊含义
  • $(this)调用命名功能不佳$this作为其唯一参数

回答by Alexey Lebedev

In jQuery event handler:

在 jQuery 事件处理程序中:

  • this- is a DOM element you assigned the event handler to
  • $(this)- is a jQuery object created from that element
  • $this- typically, a variable holding the result of $(this)
  • this- 是您将事件处理程序分配给的 DOM 元素
  • $(this)- 是从该元素创建的 jQuery 对象
  • $this- 通常,一个保存结果的变量 $(this)

More generally:

更普遍:

  • thisinside 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 thisrefers to the global object (windowin non-strict mode).

    You can find a good detailed explanation on MDN.

  • $thisis a variable name. In JavaScript variable names can start with $. Some like to use it as a prefix for variables containing jQuery objects:

    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, and thisis its argument:

    var $ = alert;
    $(this); // [object Window]
    

    $doesn't have any special meaning per se. But jQuery defines the $()function as a shorthand for jQuery(). Depending on its arguments, this function can do many different things.

  • 函数内部的this指的是调用该函数的对象或原语。当函数用作构造函数时,它指的是正在构造的新对象。任何函数之外的都是this指全局对象(window在非严格模式下)。

    你可以在 MDN 上找到一个很好的详细解释

  • $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)是一个函数调用,其中$是函数名,this是它的参数:

    var $ = alert;
    $(this); // [object Window]
    

    $本身没有任何特殊意义。但是 jQuery 将该$()函数定义为jQuery(). 根据它的参数,这个函数可以做很多不同的事情

回答by Jez

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 对象。

回答by tKe

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 (clickfor example).

但是,我假设您是在元素事件的回调上下文中询问变量(click例如)。

  • thisis the context of your handler (normally the DOM element, in the case of a DOM event handler)
  • $thisis usually used to store the result of $(this)
  • $(this)returns the jQuery object that wraps this- see the jQuery documentationfor more information.
  • this是处理程序的上下文(通常是 DOM 元素,在 DOM 事件处理程序的情况下)
  • $this通常用于存储结果 $(this)
  • $(this)返回包装的 jQuery 对象this-有关更多信息,请参阅jQuery 文档

回答by Luca Matteis

Expanding on what David said:

扩展大卫所说的话:

  • $thisis usually used to have a copy of the thisobject in the current scope. For example with var $this = this;you can use the variable $thisanywhere in the current scope and always be able to reference that object that would otherwise change if simply referenced with this... I personally dislike the $thisnaming convention and prefer something like 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 of someLongFunctionNameand because it is usually called manytimes in the code it's easier to have it be as short as possible

  • $this通常用于this在当前范围内拥有对象的副本。例如,var $this = this;您可以$this在当前范围内的任何位置使用该变量,并且始终能够引用该对象,如果简单地引用this...我个人不喜欢$this命名约定,并且更喜欢类似的东西var parentScope

  • $(this)var $ = function(){}一些框架(如 jQuery 或 PrototypeJs)使用的函数 ( )。使用它的原因是因为$很容易输入的,而不是someLongFunctionName因为它通常被称为很多在更容易的代码时代有它是越短越好