On Aug 17, 12:50 am, ron.h.h...@gmail.com wrote:
On Aug 8, 4:17 pm, RobG <rg...@iinet.net.auwrote:
On Aug 9, 7:23 am, Stevo <ple...@spam-me.comwrote:
<..>
I think the best post I've seen about the this keyword is the one here
by Mike Winter:
<URL:http://groups.google.com.au/group/co...browse_frm/thr...
The first part is about closures, the second part on this is
excellent.
With regard to the second part only, and in spite of many excellent
posts that Mike contributed, this one doesn't quite make that rating
in my book.
The first problem is the use of the term "this operator" which,
Mike explained in another post why he uses that term. I've spent some
time trying to find it but can't.
[...]
If the reference to the method is obtained without use of dot or
bracket notation, "this" is assigned a reference to the global object.
On the other hand if dot or bracket notation is used to arrive at the
reference, "this" is assigned the object at the end of the accessor
path that leads to the reference.
In other words, a function invoked as f(...) will have a "this" value
of the global object, whereas a.b.c.f(...) would have a "this" value
of c.
Perhaps most non-intuitive in the above is that the above paragraph
applies even when an inner function is called, where one might expect
the "this" value to be preserved (as it is when code under the eval
function is invoked).
So the second problem with Mike's post is that it over-complicates the
description of what one should expect of the "this" value to be by
involving the variable object, scope chain and the "new" operator[1]
in the description.
I think he's being thorough. His explanation regarding identifier
resolution explains how the this keyword of an inner function is a
reference to the global object, even if the this keyword of the outer
function is some other value.
e.g.
function foo(){
alert('foo: this == window : ' + !!(this == window));
function bar() {
alert('bar: this == window : ' + !!(this == window));
}
bar();
}
var x = {};
x.foo = foo;
x.foo(); // foo: this == window : false
// bar: this == window : true
When resolving the local variable bar, it is found on foo's variable
object and so its this keyword is set to the global object, even
though the outer function, foo, has its this keyword set to x.
If a programmer wants the inner function to have access to the outer
function's this keyword, they can do:
function foo(){
var fooThis = this;
function bar() {
// Use fooThis
}
bar();
}
or
function foo(){
function bar() {
// this = foo's this;
}
bar.call(this);
}
>
See "Objects and this" at
<URL:http://javascript.crockford.com/survey.html>
for a much more succinct, easy to grasp, description.
It is a simple description that covers the majority of cases, but it
doesn't explain why. One reason I like Mike's explanations is that
they can be read and understood in conjunction with the ECMAScript
specification [1]. I don't think Douglas Crockford's can (which isn't
a criticism, I suspect that was never his intention).
1. Richard Cornford's posts fall into the same category, though they
are often much more difficult to understand.
--
Rob