Should an object declared in a function persist after function exits?

J

Jeff

Hello,

I assigned a new object to a local variable ("req") in a function (see
below). The local variable "req" is obviously destroyed when the
function exits, but should the object referenced by the variable live
on?

It seems that it does (and I want it to), but is this correct? I
thought that local variables (and objects) are destroyed when the
function exits? I declared a callback function--function () {
processReqChange (req); };--with the variable that is called later and
it all seems to work okay.

Thank you!

function SendRequest(site,size,position,id) {

var msg="targetsite=" + encodeURI(site)+ "&id=" + encodeURI (id)+
"&targetsize=" + encodeURI (size)+"&position=" + encodeURI(position);

// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) { // Safari, Mozilla
var req = new XMLHttpRequest(); //create new local object
if (req.overrideMimeType) {
//set type accordingly to anticipated content type
//req.overrideMimeType('text/xml');
req.overrideMimeType('text/html');
}
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
var req = new ActiveXObject("Microsoft.XMLHTTP"); //create new
local object
}
else {
alert("Your browser does not support XMLHTTP")
}
if (req) {
var url_to_use=url+'?unique='+ new Date().getTime ();
req.open('POST', url_to_use, true); //Open before assign callback to
ensure IE ability to repeat process
req.onreadystatechange = function () { processReqChange (req); };
//Make a new function that calls processReqChange with this object
req.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", msg.length);
req.setRequestHeader("Connection", "close");
req.send(msg);
}
}
 
R

RobG

Jeff said:
Hello,

I assigned a new object to a local variable ("req") in a function (see
below). The local variable "req" is obviously destroyed when the
function exits,

It isn't destroyed when the function exits.

SendRequest is declared in the global scope, and is therefore
instantiated when the code is first loaded into a browser (or other
host environment). At that point, req is created since it's declared
with the var keyword inside SendRequest.

Later, when SendRequest is called, an XMLHttpRequest object is created
and a reference to it assigned to req, so it now has a value.

Still later, inside SendRequest, you execute a function statement that
assigns an anonymous function to the onreadystatechange handler of the
XMLHttpRequest object referenced by req. When you do that, the
activation object of SendRequest is added to the anonymous function's
scope chain, thereby keeping req in scope for the anonymous function
and preventing the object it references from being garbage collected
(and create a closure).

At some later time, the onreadystatechange hanlder calls the anonymous
function, which calls processReqChange(req), using req to pass a
reference to the XMLHttpRequest object created when SendRequest was
called.

Even if you hadn't done the above, req doesn't get "destroyed" when the
function ends, it just goes out of scope. In that case, the object
referenced by req may be destroyed at some later time according to the
rules for garbage collection.
but should the object referenced by the variable live on?

As long as something in scope has a reference to it, it will not be
garbage collected. This may help:

It seems that it does (and I want it to), but is this correct? I
thought that local variables (and objects) are destroyed when the
function exits?

Local variables exist as long as the objects they belong to exist.

[...]
function SendRequest(site,size,position,id) {

var msg="targetsite=" + encodeURI(site)+ "&id=" + encodeURI (id)+
"&targetsize=" + encodeURI (size)+"&position=" + encodeURI(position);

// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) { // Safari, Mozilla
var req = new XMLHttpRequest(); //create new local object
[...]

req.onreadystatechange = function () { processReqChange (req); };

When the above statement is evaluated, the right hand side creates an
anonymous function that has SendRequest's activation object (and hence
req) on its scope chain. When the onreadystatechange handler calls the
anonymous function, it uses req to pass a reference to the object to
processReqChange.

Without testing, it seems reasonable that you could use:

req.onreadystatechange = function () { processReqChange (this); };

on the basis that the anonymous function is assigned as a method of the
XMLHttpRequest object, and therefore when it's called by the
onreadystatechange event, its this value will be that object.

[...]
 

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,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top