this. Because only functions create scope, and because the context in which functions execute is mutable, what
this actually points to is confusing at first.
Let’s take a simple Java class.
Using this template we can create a new student and print out the relevant information.
Student me = new Student("Joshua", 26); System.out.println(me);
In these methods,
this refers to the instance of
Student that we created.
26. The function is always executed against the current object.
this and function context comes as a shock.
Let’s take a quick example.
Extending this example to add an asynchronous call to a hypothetical remote service checking a minimum drinking age.
The potential issue is on
this will not be pointing to the
Student object like many expect. Instead it points to the global context. Here is a slightly different method that allows us to specify the context.
Here’s what’s happening. First the
drinking_age function has lost the
age parameter and been modified to look in the current context for it instead (
this.age). Then when invoking
Laws.drinking_age we are using
.call, one of the three scope-changing functions inherent in all Function objects.
.bind(this, [, arg1, arg2, ...argN])
.call(this[, arg1, arg2, ...argN])
Each of these has a distinct role in adjusting the function context.
.bind returns a new function with new context.
.apply will execute a function in a context with an optional array to expand into arguments.
.call executes a function in a context with individual arguments.
Using this structure, we can even manipulate the contexts of anonymous functions on the fly to persist current scope without messy
var self = this; statements.