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


Groups > comp.lang.javascript > #29070

Re: Scope of "this" variable

Path csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!1.eu.feeder.erje.net!news.roellig-ltd.de!open-news-network.org!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:42:45 +0100
Organization PointedEars Software (PES)
Lines 39
Message-ID <2541123.lcmPiflTUf@PointedEars.de> (permalink)
References <87ziwtqc9t.fsf@mithlond.arda> <n5uugv$fpe$1@speranza.aioe.org> <n5uuvf$gko$1@speranza.aioe.org>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1451432566 15489 eJwFwQcBwDAMAzBKufYKp83hD2FSOhTFQCJyczuaX+mu8D59NXdd/dDDJc2sbKRBoRwOLH4johCV (29 Dec 2015 23:42:46 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Tue, 29 Dec 2015 23:42:46 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwNx8kBwDAIA7CZIDbHOBDC/iO0+onHxK7DaOByW4Sr4EkVrenGzk48KdSfxks3ZQY9dFLWn6NqNobMax9bLhXV
Cancel-Lock sha1:FGh6HhadaX2Ngp8gxDzgYhi2qgA=
X-NNTP-Posting-Host eJwNx8EBwEAEBMCWCLuhHO7ov4RkfgOj8rxO0LHYPzG5/uDW9g3YWVPLnikIkTcbGynFURH/ACm2ETw=
Xref csiph.com comp.lang.javascript:29070

Show key headers only | View raw


Joao Rodrigues wrote:

> On 29/12/2015 19:38, Joao Rodrigues wrote:
>> On 29/12/2015 17:34, Teemu Likonen wrote:
>>> I know a work-around:
>>>
>>>      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.
> 
> Oops again :-) I meant "as this_object.variable" references
> Item.variable..."

Nonsense.  “this” in the context of “Item”, therefore “this_object” too, 
refers to

  - the global object if the object referred to by “Item” (“Item”)
    is called as a global function in non-strict mode (in strict mode,
    “this” would be “undefined”)

  - the constructed Item instance if “Item” is called as a constructor

  - the object that “Item” was called as a function of through method
    transfer (“target.method = source.method; target.method(…)”), or use
    of Function.prototype.call(), Function.prototype.apply() (both since
    ES 3), or Function.prototype.bind() (since ES 5.x).

-- 
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