In JavaScript, "this" is not a variable, or a binding in the local scope. It is a keyword, so it is magic. It was a terribly idiotic design decision, because the variable you'd want to close over most of the time is "this", but since it's not a variable, you can't close over it -- it's dynamically bound, in a really stupid way.<p>It's the cause of a lion's share of JavaScript bugs. No matter how well you know JavaScript, and how pedantically you understand how "this" works and why it's such a terrible design flaw, you will ALWAYS make mistakes with it, and you will ALWAYS have to go carefully over your code line by line looking for misuses of "this".<p>Whenever your JavaScript program is misbehaving, the first thing you should do is to search the relevant code for "this", and think carefully about how it might be bound. And even if your JavaScript program is not misbehaving in a way you can detect, you should still do that anyway, because it might just have some very subtle bugs caused by "this".<p>I've been writing code that starts out with "var self = this;" and then ONLY using "self" and NEVER using "this" except for the first line of a function that goes "var self = this;". That way there's a lot less chance of making mistakes, although it's still possible for that first "this" to be incorrectly bound -- you only want to do the "var self = this;" in top level functions, and all functions contained in them should use self and never use "this".
It's whatever is to the left of the dot.<p>The exceptions:<p>1) when used with new, it's the new instance of the object (pretend that when you use the new keyword, an Object.create() call is inserted in the beginning of the constructor function),<p>2) when there's nothing to the left of the dot, it's the window object.<p>3) .call and .apply force this to be something else.
If the beginner understands functions I tell them it's the specific object running the function call. If not, I tell them it's the object that owns the most immediate scope (as in the one "running the code").
Also important here is the definition of what an object is.
The key sentence to memorize is: "An object is an instance of a class" which is to say that the two are not the same. Saying a class is a blueprint and an object is the actual house that was built according to the blueprint (the class definition)is a good way to get this through. "This" in the above analogy more easily means a reference to a specific house.