|
|
|
Next: Missing Cookies
|
| Author |
Message |
External

Since: Jul 05, 2007 Posts: 11
|
(Msg. 16) Posted: Fri Aug 17, 2007 3:43 am
Post subject: Re: this confusion Archived from groups: comp>lang>javascript (more info?)
|
|
|
On Aug 17, 2:44 am, ron.h.h... DeleteThis @gmail.com wrote:
> On Aug 16, 9:27 am, Henry wrote:
>> On Aug 16, 3:50 pm, ron.h.h... DeleteThis @gmail.com wrote:
<snip>
>>> In other words, a function invoked as f(...) will have a
>>> "this" value of the global object,
>
>> function f(){
>> var x = (this+'');
>
>> }
>
>> with(
>> {
>> f:f,
>> toString:function(){alert('I am not the global object');}
>> }
>> ){
>> f(); // alerts: I am not the global object
>> // So the - this - value when - f() - is
>> // executed is _not_ the global object.
>
>> }
>
>> You appear to have failed to take into account the role of variable
>> objects and the scope chain in determining the - this - value.
>
> So, given the Michael Winter post under discussion, what would you
> say the "The global and activation/variable objects:" section, that
> brings under consideration scope chains, activation and variable
> objects, leads to as the "this" value in your example?
The circumstances under which the - this - keyword will not be a
reference to the global object only occur if when what may be referred
to as the operand of the 'call operator' (or 'the bit before the
opening parenthesis of the arguments list', if you prefer) evaluates
as an instance of the internal Reference type. The two things that may
evaluate as instances of the internal Reference type are property
accessors and Identifiers, and the - f - in - f(); - is an Identifier
and so will (must) evaluate to a Reference type. So the question is
not why in this example the - this - keyword refers to something that
is not the global object but rather why it dose not refer to something
other than the global object with almost all other uses in the form of
- identifier() -. The answer to that is precisely what Michel Winter
is addressing when talking about activation/variable objects.
> "with", as always, is a special case
Not really. The - with - statement does something very simple; places
an object on the scope chain, but doing that has implications for the
outcome of - this - assignments by the general mechanism whenever
Identifiers are the operand of the 'call operator'
> (and that is part of the reason
> that its use is generally recommended against).
> The relevance of the
> scope chain in this case has to do with search order, and is
> incidental to the setting of the "this" value on call to a method
> found as a property of the "with" computed object.
The search order is the coincidental factor, it is only the fact that
the object added to the scope chain is not an Activation/Variable
object (unlike every other object on the scope chain at the time) that
allows the - this - reference to be something other than the global
object.
> Syntactically, you are correct,
Logically I am correct as well. The statement: "a function invoked as
f(...) will have a "this" value of the global object" is proved false
by any single empirical demonstration that contradicts it.
> that explicit accessor processing does
> not occur at the time of the call. Nonetheless, logically, the
> property when found can be thought of as
>
> withComputedObject[propertyName]
It "can be thought of as ..." if you also regard normal (without -
with - statements) Identifier resolution as the equivalent of:-
varibleObject[propertyName]
- and if you do that you need to explain why - variableOjbect - is not
the - this - value.
> , and therefore the simple rule given for determination of the
> "this" value under a call applies.
Well, no. Your "simple rule" is too simple. And it does not even
address cases such as:-
var test = 'global';
function AnObject(){
this.test = 'AnOjbect';
};
AnObject.prototype.method1 = function(){
return this.method2;
};
AnObject.prototype.method2 = function(){
alert(this.test);
};
var obj = new AnObject();
obj.method1()(); //which alerts 'global'. |
|
| Back to top |
|
 |  |
External

Since: Jun 03, 2007 Posts: 25
|
(Msg. 17) Posted: Fri Aug 17, 2007 10:22 am
Post subject: Re: this confusion Archived from groups: per prev. post (more info?)
|
|
|
On Aug 17, 3:43 am, Henry <rcornf....DeleteThis@raindrop.co.uk> wrote:
> On Aug 17, 2:44 am, ron.h.h....DeleteThis@gmail.com wrote:
>
>
<...>
>
> > So, given the Michael Winter post under discussion, what would you
> > say the "The global and activation/variable objects:" section, that
> > brings under consideration scope chains, activation and variable
> > objects, leads to as the "this" value in your example?
>
> The circumstances under which the - this - keyword will not be a
> reference to the global object only occur if when what may be referred
> to as the operand of the 'call operator' (or 'the bit before the
> opening parenthesis of the arguments list', if you prefer) evaluates
> as an instance of the internal Reference type. The two things that may
> evaluate as instances of the internal Reference type are property
> accessors and Identifiers, and the - f - in - f(); - is an Identifier
> and so will (must) evaluate to a Reference type. So the question is
> not why in this example the - this - keyword refers to something that
> is not the global object but rather why it dose not refer to something
> other than the global object with almost all other uses in the form of
> - identifier() -. The answer to that is precisely what Michel Winter
> is addressing when talking about activation/variable objects.
>
What Michael Winter was addressing is not a matter of disagreement.
You didn't answer the question.
> > "with", as always, is a special case
>
> Not really. The - with - statement does something very simple; places
> an object on the scope chain, but doing that has implications for the
> outcome of - this - assignments by the general mechanism whenever
> Identifiers are the operand of the 'call operator'
>
> > (and that is part of the reason
> > that its use is generally recommended against).
> > The relevance of the
> > scope chain in this case has to do with search order, and is
> > incidental to the setting of the "this" value on call to a method
> > found as a property of the "with" computed object.
>
> The search order is the coincidental factor, it is only the fact that
> the object added to the scope chain is not an Activation/Variable
> object (unlike every other object on the scope chain at the time) that
> allows the - this - reference to be something other than the global
> object.
>
It doesn't really matter where it's located. The differentiator is
that the "with" computed object is a program accessible object,
whereas others you mention above are not (and are not intended to be,
and that's why the specification doesn't allow them to find their way
into the "this" value).
> > Syntactically, you are correct,
>
> Logically I am correct as well. The statement: "a function invoked as
> f(...) will have a "this" value of the global object" is proved false
> by any single empirical demonstration that contradicts it.
>
> > that explicit accessor processing does
> > not occur at the time of the call. Nonetheless, logically, the
> > property when found can be thought of as
>
> > withComputedObject[propertyName]
>
> It "can be thought of as ..." if you also regard normal (without -
> with - statements) Identifier resolution as the equivalent of:-
>
> varibleObject[propertyName]
>
> - and if you do that you need to explain why - variableOjbect - is not
> the - this - value.
>
I don't see the necessity of going there, but wouldn't that in general
just be:
internalObject[propertyName] => this à globalObject under call
> > , and therefore the simple rule given for determination of the
> > "this" value under a call applies.
>
> Well, no. Your "simple rule" is too simple. And it does not even
> address cases such as:-
>
> var test = 'global';
>
> function AnObject(){
> this.test = 'AnOjbect';};
>
> AnObject.prototype.method1 = function(){
> return this.method2;};
>
> AnObject.prototype.method2 = function(){
> alert(this.test);
>
> };
>
> var obj = new AnObject();
>
> obj.method1()(); //which alerts 'global'.
Quite right.
Does the Michael Winter (non-too-simple) description, of which you
seem to be a defender, "even address cases such as:" the above? If
so, where?
--
../rh |
|
| Back to top |
|
 |  |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
|