By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

Something odd happening to 'this'

P: n/a

I have an object, a, defined in a js file, with functions b, c and d
thus:

function a()
{
function b() { ... this.sth.dosth ... }
function c() { ... this.sth.dosthelse ... }
function d() { ... c() ... }

this.b = b;
this.d = d;
}

and b and d are exposed as methods as shown.

When called externally as e.g. "x=new a(); x.b();" the function b ca
find 'this.sth' as expected, and Venkman tells me that the value o
this is the a object. However, when c (which is defined a few line
after b, definitely still within a) is called from d e.g. by the we
page going "x=new a(); x.d();", I find that the value of the 'this
inside c is the window object, just as if c were global code.

I am getting around it by coding, inside d, "c.apply( this )", but tha
seems to be an ugly hack. Have I fallen foul of some kind of Javascrip
gotcha? Is it a bug?
Cheers,
Loldemort

--
Loldemor
-----------------------------------------------------------------------
Loldemort's Profile: http://www.highdots.com/forums/member.php?userid=1
View this thread: http://www.highdots.com/forums/showthread.php?t=7097

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Loldemort wrote:
function a()
{
function b() { ... this.sth.dosth ... }
function c() { ... this.sth.dosthelse ... }
function d() { ... c() ... }

this.b = b;
this.d = d;
}

[...] [W]hen c (which is defined a few lines after b, definitely
still within a) is called from d e.g. by the web page going "x=new
a(); x.d();", I find that the value of the 'this' inside c is the
window object, just as if c were global code.

I am getting around it by coding, inside d, "c.apply( this )", but
that seems to be an ugly hack. Have I fallen foul of some kind of
Javascript gotcha? Is it a bug?


No. It's expected behaviour. The this operator value is determined by
how a method is called, not just the fact that it is a method. Nested
functions - those which are properties of a function's activation
object - will always be given a reference to the global object unless
overridden. Using call or apply is one way to get around this[1], as
would passing the this value as an argument to c or creating a
variable which holds the same value:

function a() {
var instance = this;

function c() {
/* Use instance in place of this. */
}
}

Further restructuring of the code could also tackle the issue.

Hope that helps,
Mike
[1] Beware that IE implemented them rather late. The call and apply
methods aren't available in JScript versions prior to 5.5
(typically installed with IE5.5).

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a

Yes, that's most useful thank you

--
Loldemor
-----------------------------------------------------------------------
Loldemort's Profile: http://www.highdots.com/forums/member.php?userid=1
View this thread: http://www.highdots.com/forums/showthread.php?t=7097

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.