The this variable is an important concept in JavaScript and in many Object-Oriented languages. (Technically this is a keyword, but that’s a minor point in understanding it.) The simplest way to think of this is that it references the object whose method is currently being called. (The harder, but more accurate, way of thinking of this is as a representation of the execution context, but I find that definition makes a person blink a lot while simultaneously scratching his or her head.) The brilliant Peter-Paul Koch describes this as the “owner” of the current method, which also works.
To put this into a context (double pun), take the following JavaScript code, which defines a custom object, representing a rectangle:
\[js\]\[/js\]
That code defines an object with two attributes and one method. The method, called using the code r.isSquare(), returns a Boolean value indicating if the rectangle is also a square or not. Since the isSquare() method is defined as part of the r object, within that method, this refers to r.
…