【发布时间】: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