Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29062
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Teemu Likonen <tlikonen@iki.fi> |
| Newsgroups | comp.lang.javascript |
| Subject | Scope of "this" variable |
| Date | Tue, 29 Dec 2015 21:34:54 +0200 |
| Organization | A noiseless patient Spider |
| Lines | 67 |
| Message-ID | <87ziwtqc9t.fsf@mithlond.arda> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" |
| Injection-Info | mx02.eternal-september.org; posting-host="acd0cf975b2d795ec1eea69942f310f4"; logging-data="4065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/DKIvcUUh3cALunYapj2zheMxETh6dK4=" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.5.50 (gnu/linux) |
| Cancel-Lock | sha1:4fjQ+QOHm/i/Kbe/MdAXxRfORjM= sha1:EGbCUzgJEuaHL4nTqzkIo21/ti8= |
| Xref | csiph.com comp.lang.javascript:29062 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
Code
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 {...}. Why?
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; }
};
};
But I'd like to understand why the first example works the way it does.
What's the difference between these to examples?
--
/// Teemu Likonen - .-.. <https://github.com/tlikonen> //
// PGP: 4E10 55DC 84E9 DFF6 13D7 8557 719D 69D3 2453 9450 ///
Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll 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