On Nov 12, 12:53 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> Hector's head must be spinning by now, if he's continued to read this
> thread.
I hope he(?) has, discussions are a great way get to understand
things.
> 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.
Your statement was:
"Whenever you call a function in JavaScript, it's always called as a
method
of some object, and this is a reference to that object."
Just to be clear, the term "method" is defined in ECMA-262 section 4.2
as "a function associated with an object
via a property".
All I wanted to point out was that functions are only called as
methods of objects when you specifically do that. It has a side
effect of setting the this keyword as a reference to that object. It
shouldn't be inferred that an unqualified call results in the function
being called as a method of some other object, or that the this
keyword is always set to the object that the function is called as a
method of (see below).
> 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.
But it's all over by the time you're inside (that wasn't meant to be
offensive...).
> 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*.
That's where I disagree. In your teminology, I'd say alertX has been
called as *if it was* a method of two, rather than as a method of
two. In my language, I'd just say call sets alertX's this keyword to
two and forget the word method.
[...]
> Can we agree to agree? :-)
I guess so.
--
Rob