Thanks to @elclanrs for reminding me of things I had internalized and forgotten...
That
The important thing here is that that
... is unnecessary.
I'll quote an article that @elclanrs linked in his comment on the above post:
Scope In Javascript
JavaScript establishes an execution context for the function call, setting this to the object referenced by whatever came before the last ”.”
Because each method is called with the outer Calc
before it's dot, the this
value inside that method is assigned as the outer object.
The outer object, in turn, is its own brand new, self-contained scope because it was created with the new
keyword:
When new
[Calc
]()
is executed, a completely new object is created transparently in the background. [Calc
] is called, and its this keyword is set to reference that new object.
(Scope in Javascript, again, with my edits in brackets).
Now you might be wondering, "How is this:
.add(1)
.add(2)
.multiply(3)
... keeping the right scope? You said that whatever is before the .
is passed in as the this
variable in this situation!?"
Absolutely true, and in this situation, each method is returning this
, which allows method chaining. (They're actually returning that
, but we already determined that was an unnecessary variable in this context).
Why use that
First of all, let me say I prefer var self = this
over var that = this
but there are arguments either way.
Let's arbitrarily modify the object to have a method that looks like this:
this.getInternalThis = function(){
var internalThis = function(){
console.log( this );
}
}
First of all, let's get this out of the way: this example is stupid, but you'll see things like this - a function defined in other scopes - all the time.
Here are the important things to notice:
- It's called by name, and nothing more (no prefixed
.
notation, for example)
- ... that's it!
When a function is called this way, the engine has to figure out something to assign this
as in the scope of the function. It defaults to window
.
If you were to run this code, you would get Window
in the console.
Now, what if we wanted this
inside that internal function call to be the calling value of this
?
This situation is where you need a that
variable. We can modify the function to look like:
this.getInternalThis = function(){
var that = this,
internalThis = function(){
console.log( that );
};
}
Now when you run this method, you get the value of the calling object in the console.
In my case it was Object { add=function(), multiply=function(), equals=function(), getInternalThis=function()}
.
Sometimes, that's what you need or expect, so that's why you would use a var that = this
declaration.
Using this.
to define a method
As I mentioned earlier:
Because each method is called with the outer Calc
before it's dot, the this
value inside that method is assigned as the outer object.
Remember that this
in the scope of Calc()
is a reference to the new Calc object, so each method is being given the Calc object as the value of this
(remember, it's before the .
!) when they enter their new scope from that context.
Hopefully this gives you a little info on how JavaScript scopes and the assignment of this
works.