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

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

P: n/a
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);
}
}

Oct 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jeff wrote:
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 can.

What you're describing is called a closure. Read this:
http://www.jibbering.com/faq/faq_notes/closures.html

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 18 '06 #2

P: n/a
Jeff wrote:
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:

<URL: http://www.jibbering.com/faq/faq_not....html#clIRExSc >
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.

[...]

--
Rob

Oct 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.