Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29069

Re: Scope of "this" variable

Path csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Scope of "this" variable
Date Wed, 30 Dec 2015 00:35:13 +0100
Organization PointedEars Software (PES)
Lines 62
Message-ID <4114182.jsOz3Ienk7@PointedEars.de> (permalink)
References <87ziwtqc9t.fsf@mithlond.arda> <n5uugv$fpe$1@speranza.aioe.org>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1451432113 13564 eJwNy8ERACEIBLCW4GAXKUdE+i/hfGcCo/KEE3QMRmOu3HJHSnR98izTx9SEx5Z6s6325jK8+QMN2xBt (29 Dec 2015 23:35:13 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Tue, 29 Dec 2015 23:35:13 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwNxsEBwCAIA8CVDJKI44DK/iO09zpOQWe5KGez/cqmw85g7a4L/t9moVj3ZTJHeNno2DAvPLCKajWaK/IDOAcU+A==
Cancel-Lock sha1:sqeJnUhyXBZMgMW6TIqtm6Iu9sU=
X-NNTP-Posting-Host eJwFwYkRwDAIA7CV4DB2O07Cs/8IkTLoLIFJ5OZKpQG5Azto3pYH5r+RoHVuNb/yY7WGoh4eFxE3
Xref csiph.com comp.lang.javascript:29069

Show key headers only | View raw


Joao Rodrigues wrote:

> On 29/12/2015 17:34, Teemu Likonen wrote:
>>      var Item = function() {
>>          this.variable = "variable";
>>          this.fn1 = function() { return this.variable; };
>>          this.foo = {
>>              fn2: function() { return this.variable; }
>                                         ^^^^^^
> Here, 'this.variable' means 'foo.variable',

No, it does not.

> but there isn't such identifier in foo,

Your wording is imprecise at best.  There is no “foo”, and this has nothing 
to do with identifiers.  “this.foo” in the direct execution context of Item 
is a reference to an object.  That object does not have a property with the 
name “variable”.

> so 'this.variable' will return undefined, according
> to the ECMAScript 5.1, $ 8.7 - The Reference Specification Type.

_§_, and irrelevant.

> Also see $ 10.3.1, 11.1.1 and 11.1.2:

_§§_, and irrelevant.

> <http://ecma-international.org/ecma-262/5.1/>

The relevant section there is <http://ecma-international.org/ecma-262/5.1/#sec-11.2.1>

> But if you add a line with "this.foo.variable = 'variable2'", then the
> property reference will work fine:

It is not a property reference, but a property _access_.
 
>>      var Item = function() {
>>          this.variable = "variable";
>>          this.fn1 = function() { return this.variable; };
>>          var this_object = this;
>>          this.foo = {
>>              fn2: function() { return this_object.variable; }
>                                         ^^^^^^^^^^^^
> 
> Oops, this is something else, not a workaround, as this_object
> references Item.variable, not foo.variable as in the previous case.

Nonsense.

> IMO, "Kangax" wrote a masterpiece about the 'this' keyword in
> ECMAScript. You may want to take a look:
> <http://perfectionkills.com/know-thy-reference/>

Unless you have your misconceptions from there, go read it.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Scope of "this" variable Teemu Likonen <tlikonen@iki.fi> - 2015-12-29 21:34 +0200
  Re: Scope of "this" variable Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-29 20:47 +0000
    Re: Scope of "this" variable Teemu Likonen <tlikonen@iki.fi> - 2015-12-30 10:57 +0200
  Re: Scope of "this" variable Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-29 22:09 +0100
  Re: Scope of "this" variable Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-12-29 19:38 -0200
    Re: Scope of "this" variable Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-12-29 19:46 -0200
      Re: Scope of "this" variable Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-30 00:42 +0100
    Re: Scope of "this" variable Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-30 00:35 +0100
    Re: Scope of "this" variable Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-12-30 02:18 -0200

csiph-web