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


Groups > comp.lang.javascript > #29065

Re: Scope of "this" variable

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Scope of "this" variable
Date 2015-12-29 22:09 +0100
Organization PointedEars Software (PES)
Message-ID <1477065.DiSW4fkXl8@PointedEars.de> (permalink)
References <87ziwtqc9t.fsf@mithlond.arda>

Show all headers | View raw


Teemu Likonen wrote:

>     var Item = function() {
>         this.variable = "variable";
>         this.fn1 = function() { return this.variable; };
>         this.foo = {
>             fn2: function() { return this.variable; }
>         };
>     };
> 
>     var item1 = new Item();
>     console.log(item1.fn1());
>     console.log(item1.foo.fn2());
> 
> prints this:
> 
>     "variable"
>     undefined
> 
> So function item1.fn1 returns the string "variable" but function
> item1.foo.fn2 returns undefined. The variable "this" somehow disappears
> (or doesn't last long enough) when a function object is created inside
> another object {...}.

“this” is not a variable; it is a value.  And it does not appear or 
disappear at all.

> Why?

You have stored in “this.foo” a reference to an object (here: an Object 
instance, created with the Object initialiser, “{}”).  “this” is only 
resolved when a function or method is called, and the object referred to by 
“this.foo” (on which you are calling the “fn2” method) neither has nor 
inherits a property named “variable”.  Accessing a non-existing property 
usually (without Proxy) yields the “undefined” value of the Undefined type.
 
> 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; }
>         };
>     };

Yes, that is a proper workaround.  People also use “that” or “self” instead 
of the longer “this_object”.
 
However, unless you are actually referring to a variable declared in the 
local context of the constructor (which can be understood as a private 
property since only the public methods of the instance or its prototype, if 
defined within the constructor, can access it – they are then called 
“privileged”), or the method changes significantly depending on the 
constructor call in some other way, you should avoid defining public methods 
in the constructor and define them as prototype methods instead, saving 
memory (at the expense of runtime efficiency due to the prototype chain 
lookup).

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