On Jan 2, 2011, at 4:07 PM, Garrett Smith wrote:
> On 1/2/11, Lasse Reichstein <[email protected]> wrote:
>> On Thu, 30 Dec 2010 14:48:44 +0100, Tom Wilson <[email protected]>
>> wrote:
>>
>>> I just finished reading "High Performance JavaScript" and now I'm paying
>>> more attention to scope. So my question is: within an object method, is
>>> there any benefit to declaring a local variable referencing 'this'? Is
>>> that just redundant?
>>
>> It's redundant at best. At worst it can lead to a small slowdown.
>> The "this" operator is special and will always refer to the same value
>> during a call, and that value is (in ES3) known to be an object.
>>
> IIRC, old versions of IE had a miniscule, negligible improvement with
> aliasing a local variable.
>
> (`this` is a keyword, not an operator, BTW).
>
>> That means that usages of "this" can omit some checks that a variable
>> use would require (e.g. "this.method()" doesn't need to check whether
>> "this" refers to a primitive value that needs to be promoted to an object).
>>
>
> Is this optimization limited to non-strict code?
>
> The reason I ask is that ES 5 s 10.4.3 "Function Calls" mentions that
> the `this` value can be null or undefined, both of which are primitive
> values.
10.4.3 Entering Function Code
The following steps are performed when control enters the execution context for
function code contained in function object F, a caller provided thisArg, and a
caller provided argumentsList:
1. If the function code is strict code, set the ThisBinding to thisArg.
2. Else if thisArg is null or undefined, set the ThisBinding to the global
object.
3. Else if Type(thisArg) is not Object, set the ThisBinding to
ToObject(thisArg).
4. Else set the ThisBinding to thisArg.
5. Let localEnv be the result of calling NewDeclarativeEnvironment passing
the value of the [[Scope]] internal property of F as the argument.
6. Set the LexicalEnvironment to localEnv.
7. Set the V ariableEnvironment to localEnv.
8. Let code be the value of F’s [[Code]] internal property.
9. Perform Declaration Binding Instantiation using the function code code
and argumentList as described in 10.5.
On Step 1-3 explains what should happen. at the beginning its the process to
set the this value. if its passed null or undefined then you should get the
global object. if its a primitive ten convert it to an object.
So I assume that by the time you use the PrimaryExpression this its resolved to
an object and no further check is needed because it has been defined at the
start of the execution process for that function.
>
> For example, in BESEN IDE, the `this` value is undefined in strict mode.
>
> "use strict";
> (function() {
> alert(this);
> })();
>
> alerts "undefined";
>
> [...]
When i tested this on Firefox and Chrome i get window. I don't know BESEN IDE
but what is the global object on it?
> --
> Garrett
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]