![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
Jorge wrote: Leaving aside .apply() / .call() and new (Constructors), isn't it that the simple rule to remember is that : The value of 'this' is preset whenever a function is entered, and, 'this' is always preset to the global object *** except when/if the function is called as an object's method *** ? No, this would mean that the Global Object was not an object. It could also mean that there were instances where `this' was not preset |
#12
| |||
| |||
|
|
Thomas 'PointedEars' Lahn wrote: Jorge wrote: Leaving aside .apply() / .call() and new (Constructors), isn't it that the simple rule to remember is that : The value of 'this' is preset whenever a function is entered, and, 'this' is always preset to the global object *** except when/if the function is called as an object's method *** ? No, this would mean that the Global Object was not an object. It could also mean that there were instances where `this' was not preset Hmm, why ? |
#13
| |||
| |||
|
|
ECMA 262 (usually 3rd Ed (for now (3.1 is planed for next year))). No other source is definitive. |
#14
| |||
| |||
|
|
(...) Neither call looks like what could be named a "method call", yet the - test2('2'); - call is setting the - this - value to the object added to the scope chain, so it must be a "method call" in some sense. And if - test('2'); - is a method call then how can - test1('1'); - not be considered to be one? (...) |
|
(...) The distinction certainly cannot be observed from the CallExpressions themselves. |
#15
| |||
| |||
|
|
No, this would mean that the Global Object was not an object. |
|
It could also mean that there were instances where `this' was not preset (...) Because one could read your statement-question as "The value of 'this' is preset whenever [something applies] *** except when/if the function is called as an object's method ***". |
#16
| |||
| |||
|
|
Thomas 'PointedEars' Lahn wrote: No, this would mean that the Global Object was not an object. No, this means that calling f() is not necessarily the same as calling window.f(), but 'this' will point to 'window' in both cases (except sometimes within a 'with', as Henry has wisely pointed out). |
|
It could also mean that there were instances where `this' was not preset (...) Because one could read your statement-question as "The value of 'this' is preset whenever [something applies] *** except when/if the function is called as an object's method ***". 1.- The value of 'this' is preset whenever a function is entered, |
|
and, 2.- 'this' is always preset to the global object *** except when/if the function is called as an object's method *** ? |
#17
| |||
| |||
|
|
Jorge wrote: snip and, 2.- 'this' is always preset to the global object *** except when/if the function is called as an object's method *** ? Incorrect, because a function is *always* called as an object's method, ... snip |
#18
| |||
| |||
|
|
Nonsense. *What "Henry" pointed out instead was that without an identifier as base of the reference the scope chain matters, especially if there is no property accessor syntax at all. *The `with' statement is but an example of that |
|
It could also mean that there were instances where `this' was not preset (...) Because one could read your statement-question as "The value of 'this' is preset whenever [something applies] *** except when/if the function is called as an object's method ***". 1.- The value of 'this' is preset whenever a function is entered, If you say "a function *execution context*", this is correct. and, 2.- 'this' is always preset to the global object *** except when/if the function is called as an object's method *** ? Incorrect, because a function is *always* called as an object's method, even though identifier resolution has to work along the scope chain, and may only find a property with that name as one of the Global *Object*. |
#19
| |||
| |||
|
|
Incidentally, there are plans to change the way - this - behaves in ES 3.1 (the next planned spec version, as ES 4 is on the backburner again) such that - this - may actually be null or undefined in some circumstances. |
#20
| |||
| |||
|
|
Thomas 'PointedEars' Lahn wrote: Nonsense. What "Henry" pointed out instead was that without an identifier as base of the reference the scope chain matters, especially if there is no property accessor syntax at all. The `with' statement is but an example of that I can't figure out another case in which a call to f() would preset 'this' to something !== window ? |
|
Could you show me how, when ? (Not using 'with', .call() nor .apply()) |
|
2.- 'this' is always preset to the global object *** except when/if the function is called as an object's method *** ? Incorrect, because a function is *always* called as an object's method, even though identifier resolution has to work along the scope chain, and may only find a property with that name as one of the Global *Object*. Let's say that f= object.method= function () { ... } Called as an object's method : object.method() Called as a function : f() |
![]() |
| Thread Tools | |
| Display Modes | |
| |