【问题标题】:What is the reason for var $this = thisvar $this = this 的原因是什么
【发布时间】:2011-11-06 06:00:46
【问题描述】:

我不是最擅长 jquery,我遇到了一个 var 初始化,我不知道编写代码的人为什么这样做。

在插件的初始化中,我们有

this.init = function(settings) {
    var $this = this;
    this.s = {
        initialSlide: 0,
        firstSlide: true,
    };
   ... more code, some uses $this, some uses "this"
}

那么“$this”和“this”之间有什么区别,为什么不一直使用其中一个呢?

【问题讨论】:

  • 是的,它是一个 jquery 成语
  • @Matt:代码中没有 jQuery。将this 分配给另一个变量在“纯 JavaScript”中也很常见。
  • @Felix:它是一个插件,也就是说 this 指的是一个 jquery 对象。有(或曾经有)一个 jquery 成语用 $ 作为 jquery 对象的前缀。因此,它是一个 jquery 问题。如果它不是 jquery,你可能会看到 $this 而不是那个,self,或者更能描述 this 实际含义的东西
  • @Matt: 哦... 在插件的初始化中 .... 我错过了那部分。 NVM。

标签: javascript jquery variables this


【解决方案1】:

通常,这意味着this 的副本。 this 的问题在于它在每个函数中都会发生变化。但是,以这种方式存储它可以防止 $this 发生变化,而 this 确实会发生变化。

jQuery 大量使用神奇的this 值。

考虑一下这段代码,你可能需要像你看到的那样:

$.fn.doSomethingWithElements = function() {
    var $this = this;

    this.each(function() {
        // `this` refers to each element and differs each time this function
        //    is called
        //
        // `$this` refers to old `this`, i.e. the set of elements, and will be
        //    the same each time this function is called
    });
};

【讨论】:

  • 对于旧版本的 jQuery 你如何写 $this ? jQuery这个??
  • var $this 就像任何其他变量名称一样,$ 和 this 只是让另一个阅读代码的人知道该变量在页面下方的作用的一种方式。简而言之,是的,您可以写var jQuerythis,但这并没有什么区别。您也可以在旧 jQuery 中使用 $,如上所述 $this 不依赖于 jQuery,它只是创建this 副本的一种方式。
【解决方案2】:

在这种情况下,什么都没有。 $this 只是另一个变量声明,它分配了this

通常,我看到使用 JavaScript 库的人在包装 this 时使用此快捷方式。例如,jQuery 中的典型用法是:

// rather than writing $(this) everywhere
var $this = $(this);

$this.each(function(){
    // Do Something
});

【讨论】:

    【解决方案3】:

    实际上,jQuery 是 JavaScript DOM 的包装器,既增强又简化了它。 非常简要地,JQuery 选择器返回 JQuery 对象,即

    var jQueryResults = $("article"); //Contains all article elements in the DOM as JQuery objects
    

    但是,使用 Javascript 选择元素会返回 HTML DOM 元素,即

    var normalResults = document.getElementsByTagName("article");//Contains all article elements in the DOM as HTML objects
    

    问题在于 DOM 对象不提供与 JQuery 对象相同的功能。

    这是一个说明差异的事件示例:

    $('.changeColorHover').hover(function() {
        this.attr("style", "color:red");
    }); //Will not work as we are trying to call a JQuery method on a DOM object
    

    考虑到上面提到的“this”关键字是一个 DOM 对象,因此您需要将其转换为 jQuery 对象才能使用 jQuery 方法。

     $('.changeColorHover').hover(function() {
            $(this).attr("style", "color:red");
     }); //Will work since we have first converted the DOM object to a JQuery object
    

    总而言之,this 关键字允许您访问调用事件的对象,因为 this 将引用引发事件的对象。但是,这是一个 DOM 对象,而不是 jQuery 对象。因此,您想使用的任何 jQuery 方法都不可用,除非您将其转换为 jQuery 对象。

    【讨论】:

      【解决方案4】:

      在这种情况下没有任何意义(没有双关语)。如果语句是 var $this = $(this) 会更合乎逻辑,因为这将允许使用所有漂亮的 jQuery 功能。

      【讨论】:

      • 不正确,将其重新分配给不同的变量允许内部闭包引用外部范围。
      【解决方案5】:

      正如其他人所说,在 jquery 代码中也有一个成语用 $ 前缀 jquery 对象。不知道现在有多火了,以前看过很多。

      【讨论】:

        【解决方案6】:

        我这样做 $this = $(this) 因为它似乎会在您每次想要使用它时保存该调用的实际处理。

        另外,对于其他人提到的'magic 'this'' 功能。保留原始副本很方便。

        【讨论】:

          猜你喜欢
          • 2013-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-20
          相关资源
          最近更新 更多