Javascript var $this = this 的原因是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7234282/
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
What is the reason for var $this = this
提问by hogsolo
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.
我不是 jquery 的佼佼者,我遇到了一个 var 初始化,我不知道编写代码的人为什么这样做。
In the init for a plugin, we have
在插件的初始化中,我们有
this.init = function(settings) {
var $this = this;
this.s = {
initialSlide: 0,
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?
那么“$this”和“this”之间有什么区别,为什么不一直使用一个或另一个?
回答by pimvdb
Generally, this means a copy of this
. The thing about this
is that it changes within each function. Storing it this way, however, keeps $this
from changing whereas this
does change.
通常,这意味着this
. 问题this
在于它在每个功能内都发生了变化。然而,以这种方式存储它不会$this
改变而this
确实会改变。
jQuery heavily uses the magic this
value.
jQuery 大量使用魔法this
值。
Consider this code, where you might need something like you are seeing:
考虑此代码,您可能需要在其中看到类似的内容:
$.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
});
};
回答by Justin Niessner
In this case, nothing. $this
is just another variable declaration which has this
assigned to it.
在这种情况下,什么都没有。$this
只是this
分配给它的另一个变量声明。
Typically, I've seen this shortcut used by people using JavaScript libraries when wrapping this
. For example, typical usage in jQuery would be:
通常,我见过使用 JavaScript 库的人在包装this
. 例如,jQuery 中的典型用法是:
// rather than writing $(this) everywhere
var $this = $(this);
$this.each(function(){
// Do Something
});
回答by Shaun
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.
实际上,jQuery 是 JavaScript DOM 的包装器,既增强又简化了它。非常简短的 JQuery 选择器返回 JQuery Object/s 即
var jQueryResults = $("article"); //Contains all article elements in the DOM as JQuery objects
However, selecting elements with Javascript returns HTML DOM elements i.e.
但是,使用 Javascript 选择元素会返回 HTML DOM 元素,即
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.
问题在于 DOM 对象提供的功能与 JQuery 对象提供的功能不同。
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.
考虑到上述内容,'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
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.
总之,this 关键字允许您访问调用事件的对象,因为这将提供对引发事件的对象的引用。但是,这是一个 DOM 对象,而不是一个 jQuery 对象。因此,您想使用的任何 jQuery 方法都不可用,除非您将其转换为 jQuery 对象。
回答by josh.trow
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.
在这种情况下它没有任何意义(没有双关语)。如果该语句var $this = $(this)
允许使用所有漂亮的 jQuery 功能,那将更合乎逻辑。
回答by Matt Briggs
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.
其他人所说的,jquery 代码中也有一个习惯用法,用 $ 前缀 jquery 对象。不知道现在流行到什么程度,但以前看过很多。
回答by Brent Rittenhouse
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.
我这样做 $this = $(this) 是因为它似乎每次要使用它时都会保存该调用的实际处理。
Also, for the 'magic 'this'' function that someone else mentioned. It is handy to keep the original copy around.
另外,对于其他人提到的“魔法'这个'”功能。保留原始副本很方便。