'this' going out of scope

E

Emanuele D'Arrigo

Hi everybody,

in the small snippet of code below, you can see two identical
debugMsg(this.myname) statements, one correctly returns "manu"
but the other returns "undefined". This is due to 'this' getting out
of scope. Is there a way I can retain the 'this' object inside the
function() block? So that I can affect the object calling this method
from there? Any workarounds instead?

Here's the code:

----------------------------------------------
function AreasManager()
{
this.myname = "manu";
}

AreasManager.prototype.addAreasFromXmlFile = function(xmlFile_)
{
debugMsg(this.myname);
var request = GXmlHttp.create();
request.open("GET", xmlFile_, true);
request.onreadystatechange = function()
{
if (request.readyState == 4)
{
var xmlContent = request.responseXML;
debugMsg(this.myname);
}
}
request.send(null);
}
 
R

RobG

Emanuele said:
Hi everybody,

in the small snippet of code below, you can see two identical
debugMsg(this.myname) statements, one correctly returns "manu"
but the other returns "undefined". This is due to 'this' getting out
of scope. Is there a way I can retain the 'this' object inside the
function() block? So that I can affect the object calling this method
from there? Any workarounds instead?

The value of the this keyword is determined entirely by how you call a
function, not by how you declare it. Try this post:

<URL:
http://groups.google.com.au/group/c...eyword+xmlhttprequest&rnum=2#5b78eba47a1a7dc9
Or search for the recent thread with subject:

Assigning methods to objects, and assigning onreadystatechange to an
XMLHttpRequest -- an inconsistency?
Here's the code:

----------------------------------------------
function AreasManager()
{
this.myname = "manu";
}

AreasManager.prototype.addAreasFromXmlFile = function(xmlFile_)
{
debugMsg(this.myname);

The this value here refers to an instance of an AreasManager object
*provided* it is called as a method of an AreasManager object.

var request = GXmlHttp.create();
request.open("GET", xmlFile_, true);
request.onreadystatechange = function()
{
if (request.readyState == 4)
{
var xmlContent = request.responseXML;
debugMsg(this.myname);

It a common assumption that in the above case, the this value of the
anonymous function refers to the request object, but as the song says,
"it ain't necessarily so". The link above gives the reason why.
 
E

Emanuele D'Arrigo

Rob, thanks for your help.

This closure thing is giving me an headache. I've gone through quite
a few posts and webpages on the matter and I feel fairly confused now.
I understand why it doesn't work, but I still can't quite understand
how
to make it work or how to change my approach to work around it.

I.e, now I tried this:

function AreasManager()
{
this.myname = "manu";
}

AreasManager.prototype.addAreasFromXmlFile = function(xmlFile_)
{
debugMsg(this.myname + "1");

var request = GXmlHttp.create();
request.open("GET", xmlFile_, true);

var self = this;
var req = request;
function handler()
{
debugMsg(self.myname + "2");
if (req.readyState == 4)
{
debugMsg(self.myname + "3");
}
}

request.onreadystatechange = handler();
request.send(null);
}

The first two debugMsg() statements work fine and will print out
the expected manu1 and manu2. But the flow never gets to
the third debugMsg() statement, because it's now the req object
to be out of scope. Why is it?

Shouldn't var req = request; act as var self = this; ??

What am I missing?

Thanks again for your help.

Manu
 
M

Michael Winter

Emanuele said:
This closure thing is giving me an headache. I've gone through quite
a few posts and webpages on the matter and I feel fairly confused now.
I understand why it doesn't work, but I still can't quite understand
how to make it work or how to change my approach to work around it.

You're alterations are almost correct, but you introduced a new error
not present in your original post.
AreasManager.prototype.addAreasFromXmlFile = function(xmlFile_)
[snip]

var req = request;

This is unnecessary, by the way. Simply use request - both are local
variables, and both will be available within the scope chain of the
function that follows. All this does is add a second reference.
function handler()

There's no need for this to be included as a function declaration. A
function expression, as in the original, will do.

[snip]
request.onreadystatechange = handler();

Your problem is here. Instead of assigning a reference to the function
to the property, you call the function and assign the return value.

[snip]
The first two debugMsg() statements work fine and will print out
the expected manu1 and manu2. But the flow never gets to
the third debugMsg() statement, because it's now the req object
to be out of scope. Why is it?

Because when the function is called, the readyState property isn't 4.
You should have checked that. :)

[snip]

Mike
 
E

Emanuele D'Arrigo

Aaaah!!! Thank you Michael!
At last I managed to come up with a working method:

AreasManager.prototype.addAreasFromXmlFile = function(xmlFile_)
{
var self = this;

var request = GXmlHttp.create();
request.open("GET", xmlFile_, true);
request.onreadystatechange = function()
{
if (request.readyState == 4)
{
var xmlContent = request.responseXML;
self.addAreasFromXmlElement(xmlContent);
}
}

request.send(null);
}

Thank you very much, this seems to work exactly as
I intended! In fact it's extremely similar to what I had
in the first place, except for the use of 'self'. Thank you
again.

Manu
 
L

Laurent Bugnion

Hi,
Aaaah!!! Thank you Michael!
At last I managed to come up with a working method:

AreasManager.prototype.addAreasFromXmlFile = function(xmlFile_)
{
var self = this;

var request = GXmlHttp.create();
request.open("GET", xmlFile_, true);
request.onreadystatechange = function()
{
if (request.readyState == 4)
{
var xmlContent = request.responseXML;
self.addAreasFromXmlElement(xmlContent);
}
}

request.send(null);
}

Thank you very much, this seems to work exactly as
I intended! In fact it's extremely similar to what I had
in the first place, except for the use of 'self'. Thank you
again.

Manu

I would recommend using against "self", because in web-browser based
JavaScript, "self" means the same as "window", it's a predefined name.
It's hidden by the local "var" declaration, but it will confuse your
audience.
http://developer.mozilla.org/en/docs/DOM:window.self

Greetings,
Laurent
 
E

Emanuele D'Arrigo

Laurent said:
I would recommend using against "self", because in web-browser based
JavaScript, "self" means the same as "window", it's a predefined name.

I was wondering why my editor did highlight it... thanks Laurent, will
do.

Manu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top