Path: csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Scope of "this" variable Date: Tue, 29 Dec 2015 22:09:16 +0100 Organization: PointedEars Software (PES) Lines: 65 Message-ID: <1477065.DiSW4fkXl8@PointedEars.de> References: <87ziwtqc9t.fsf@mithlond.arda> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1451423357 15008 eJwFwYEBACAEBMCVqPcYJ8r+I3Rnm8p20AgbG4RufyAQfRnoSkXG6bvERZ5orhajJqpO4AP/2RAR (29 Dec 2015 21:09:17 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 29 Dec 2015 21:09:17 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwVx9sBwEAEBMCWzmFRjkf0X0KSvxllENoECtHVHRma4eP3Sft9ExrRRXSsNegKbYErx92w6I02/wqpbH0BV0AV8w== Cancel-Lock: sha1:onhDyw/6z/mAZd2GP5DeFQ2iKf8= X-NNTP-Posting-Host: eJwNysERwDAIA7CVSsEmjFNCvP8IzVN3gtO4MwgGBMXLOrsc6pjOb5sd3aIxDJd1Pyr1tefk8h8oQxGi Xref: csiph.com comp.lang.javascript:29065 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: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.