In jquery, what does this
means and when it is used?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
The this Keyword
The value of this, when used in a function, is the object that "owns" the function.The value of this, when used in an object, is the object itself. The this keyword in an object constructor does not have a value. It is only a substitute for the new object.The value of this will become the new object when the constructor is used to create an object.
Note that this is not a variable. It is a keyword. You cannot change the value of this.
this
in JavaScript is very special and powerful. It can mean just about anything. I cover some of it here and here, but it's really worth finding a good tutorial on JavaScript and spending some time with it.Let's look at jQuery's use of it first, then talk about it more generally in JavaScript (a bit).
In jQuery, specifically
In code written with jQuery,
this
usually refers to the DOM element that's the subject of the function being called (for instance, in an event callback).Example jQuery event callback (what
this
is is covered in the.bind
docs):Similarly, various jQuery functions that act on all of the elements matched by the current jQuery selector can optionally accept a function, and when that function gets called,
this
is again the DOM element in question — for instance, thehtml
function allows this:Another place jQuery uses
this
is in the callback onjQuery.each
:...which will alert "one", then "two", then "three". As you can see, this is a totally different usage of
this
.(Confusingly, jQuery has two functions called
each
, the one above which is on the jQuery/$ function itself and always called that way [jQuery.each(...)
or$.each(...)
], and a different one on jQuery instances [objects] rather than the jQuery/$ function iself. Here are the docs for the other one, I don't discuss the other one in this answer because it usesthis
the same wayhtml
and event callback do, and I wanted to show a different use ofthis
by jQuery.)Generically in JavaScript
Update: As of ES5's strict mode, that's no longer true,this
refers to an object.this
can have any value. The value ofthis
within any given function call is determined by how the function is called (not where the function is defined, as in languages like C# or Java). The most common way to set upthis
when calling a function is by calling the function via a property on the object:There, because we called
foo
via a property onobj
,this
was set toobj
for the duration of the call. But don't get the impression thatfoo
is in any way married toobj
, this works just fine:In fact,
foo
isn't intrinsically tied to any object at all:There, since we didn't call
f
via an object property,this
wasn't explicitly set. Whenthis
isn't explicitly set, it defaults to the global object (which iswindow
in browsers).window
probably doesn't have a propertyfirstName
, and so we got "undefined" in our alert.There are other ways to call functions and set what
this
is: By using the function's.call
and.apply
functions:call
setsthis
to the first argument you give it, and then passes along any other arguments you give it to the function it's calling.apply
does exactly the same thing, but you give it the arguments for the function as an array instead of individually:Again, though, there's a lot more to explore about
this
in JavaScript. The concept is powerful, a bit deceptive if you're used to how some other languages do it (and not if you're used to some others), and worth knowing.Here are some examples of
this
not referring to an object in ES5's strict mode:Output:
Whereas in loose mode, all of those would have said
typeof this = object
; live copy.Top Google result for "javascript this": http://www.quirksmode.org/js/this.html
Edit: I think the key sentence is:
"In JavaScript "this" always refers to the “owner” of the function we're executing, or rather, to the object that the function is a method of."
Quirksmode is (generally*) excellent, worth reading the whole article in detail.
*Apparently this statement is not necessarily correct, see T.J. Crowder's comment below.
Here is how i would explain it, simply:
this
refers to theobject
that called thefunction
.so looking at this:
the bar object stores a reference of foo's log method into it's own log property for itself. now when bar calls its log method, this will point to bar because the method was called upon by the bar object.
this works for any other object, even the window object. if you call a function via the global scope, this will point to the window object.
by using the bind or call methods of a function, you can explicitly define what the object
this
will refer to during execution.Fun fact: anything defined in the
global scope
, which is the top layer/level, becomes a property of thewindow object
(global scope = window object).The keyword this acts as a placeholder, and will refer to whichever object called that method when the method is actually used in Java Script