JavaScript's this (current instance) keyword refers to different objects in different circumstances
(the execution context and the caller of the function).
I needed a table of these cases in one place thus this webpage.
The value of this will always be an object (some object).
Following is a list of the kinds of objects that this refers to,
with the cases that result in that reference.
Information about MooTools could be the same or different for other frameworks, toolkits, or libraries.
Please let me know of any errors or issues.
The object of which the function/method is a member or for which it is the constructor function.
Within the body of a function created as a member/property of an object when the function is invoked as a method of that object.
This includes the body of an instance method invoked as an instance method. (Class methods are special: see case 4.)
Within the body of a constructor function.
The global object (which is the window object in client-side JavaScript).
In expressions, assignment statements, and function invocation arguments at top-level.
In the global context, this refers to the global object.
Within the body of a function declared in the global context.
(For top-level function declarations, the global object is the object of which that function is a member and this subcase is
consistent with case 1.)
Within the body of a function invoked as a function not as a method, whether the function invocation is at top-level or not,
even if the function definition is nested within some other method.
(Nested functions always have the global object as their default this value, no matter where they appear.)
Within the body of a native event handler function in Internet Explorer (not with MooTools).
The object (e.g., DOM element) on which an event occurs.
Within the body of an event handler function, except natively for Internet Explorer (not using MooTools).
This is actually a special case of case 1 because the event handler function
is set as, and invoked as, a method of that DOM element object.
The class constructor function (i.e., the class).
Within the body of a static/class method.
The function prototype object.
Within the body of a function defined from the prototype when the function is called directly from the prototype.
We can re-bind the this keyword for a given function call using
JavaScript's call() or apply() methods.
For MooTools, we can also use the bind() method,
the thisArg argument to types' generic functions (static/class methods), or the
optional bind argument to many instance methods and static/class methods.
When passing this as an argument to a function invocation of a function that you write,
the formal parameter name in the function definition should not be this:
use that or some other non-reserved identifier.