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.

For example, in BESEN IDE, the `this` value is undefined in strict mode.

"use strict";
(function() {
  alert(this);
})();

alerts "undefined";

[...]
-- 
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]

Reply via email to