473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Something odd happening to 'this'


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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Betty Perea | last post by:
I'm having a little weird condition. I have a simple form (POST method), whose Action is a validation script, most of the times it works. But sometimes, the validation script does not "receive"...
5
by: Phil Latio | last post by:
I have a number of basic PHP books but they don't cover passing variables into an HTML template which is called into a PHP script. (I am trying to keep the mark-up and the execution code seperate.)...
14
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received...
0
by: sdowney717 | last post by:
Select * from something where field1 = N'literal' What exactly is happening when a query is sent using the N in front of the string to be found? Under what conditions would someone use the N'...
7
by: Gapingwound | last post by:
I have one page that is not validating XHTML1 STRICT properly, but the validator at w3c is showing an error for something that is not in my page. Here's the error: 1. Line 72, column 160:...
6
by: Andrew Mueller | last post by:
Hello all, There is a message below 'Application Error... When querying data'. What appears to be happening is that when I perform a lot of queries in a row and bring back the information into...
0
by: harrylmh | last post by:
Hi, I found something strange about the mouse's X,Y coordinates. Here's a program I made for testing. Start by moving your mouse over the Label on the left, then...move your mouse slowly from left...
4
by: Proc | last post by:
I am hoping someone here can point me in the right direction with a problem my user's are experiencing. Here is what is happening: I have an ASP.NET application that several users will use at...
1
by: Holly | last post by:
One of my clients called me this morning, having trouble with their blog loading. The blog does actually load, but only after such an unusually long wait it would have surely driven everyone else...
2
by: kigoobe | last post by:
I am having a very weird issue here, a simple form submit not working. Trying for several hours now ... must be something stupid ... help appreciated. First, the html - <form id="formFinale"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.