Hector's head must be spinning by now, if he's continued to read this
thread.

I tried to give an easy way to understand how "this" works, from the point
of view of the code that is running inside a function.

> The assertion was that a call using an unqualified identifier 
> in the global scope was the same calling the same identifier 
> as a property of the window object - it isn't, although for 
> all practical purposes the result is about the same.

No, that isn't what I said at all. No offense, but you seem to have read a
lot into what I said that just wasn't there.

Specifically, I wasn't talking about how you get a reference to a function,
only what the world looks like once you're *inside* that function.

Let's take another example:

    function One() {
        this.x = 'one here';
        this.alertX = function() { alert(this.x); };
    }

    var one = new One;

    var two = { x: 'two here' };

    one.alertX();

    one.alertX.call( two );

Obviously, one.alertX() is calling alertX as a method of the one object.

Perhaps less obviously, one.alertX.call( two ) is calling alertX *as a
method of the two object*.

It doesn't matter at all that alertX is actually a *property* of the one
object; by the time you get into the alertX function itself, that
information will be long gone. The only thing that alertX() knows is "what's
my 'this'?" That is the object which the function has been called as a
method of.

One function may be used as a method of many different objects; it doesn't
have to be a property of an object to be called as a method of that object.

Is that an unreasonable way to look at things?

Now, everything you said about scope chains and prototype chains and how you
get a reference to a function is true. No argument there. It just doesn't
have any bearing on the point I was making, which is how to understand
"this" from the context of the code running inside a function - after the
function reference has been obtained and called.

Can we agree to agree? :-)

-Mike

Reply via email to