2

I'm quite new to Javascript and I was just reading following article.

you can define an ajax connection once, and reuse it multiple times, and start and stop it later on. Here's an example:

var myAjaxRequest = A.io.request('test.html', {
    method: 'POST',
    data: {
      key1: 'value1'
    }
});

Now later on, if I want to make that same ajax call again, all I have to do is call:

myAjaxRequest.start();

What if I had a very frequently used auction page and I wanted to use the myAjaxRequest connection for all actions a user does from his browser. What are the rules for lifetime of the myAjaxRequest instance ? I suppose it is destroyed on page refresh. But is it anything else that destroys it ? Let say that the object is created within YUI sandbox, but it doesn't matter.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
lisak
  • 21,611
  • 40
  • 152
  • 243
  • 1
    No. If you don't change the `myAjaxRequest` object manually, it will be available for the lifetime of the document. (assuming that the variable is a global variable) – Šime Vidas Dec 12 '10 at 14:36
  • 1
    Set myAjaxRequest = null. It should do gc sometime depends on browser. – WaiLam Dec 12 '10 at 14:38
  • And what about time, browser memory management or something. I think in this case the variable is within a scope of a callback function of YUI sandbox. It loads an instance of a module for this variable. But let suppose it's a global variable.. – lisak Dec 12 '10 at 14:40
  • You can read this link for more info about gc in javascript. http://stackoverflow.com/questions/864516/what-is-javascript-garbage-collection – WaiLam Dec 12 '10 at 14:43
  • Ok, thank you guys, I think I got it – lisak Dec 12 '10 at 15:00
  • 3
    So why aren't any of these _answers_ actual answers instead of just comments? :P – Jani Hartikainen Dec 12 '10 at 15:13

1 Answers1

2

Its a shame this was answered in comments because nobody gets closure (sorry, terrible pun). @Šime Vidas and @WaiLam deserve the credit but I will at least attempt to craft an answer:

While you have a reference to the object (though the variable myAjaxRequest) it will remain in memory until the document is unloaded. If you assign null to your variable (myAjaxRequest = null), and there are no other references to the object, then the garbage collector will reclaim the memory used to store it.

A reference can exist even if myAjaxRequest is a local variable within a function. The function can return a reference to the local variable, for example as a object property e.g:

function sandbox () {
    var myAjaxRequest = A.io.request(/* constructor... */);

    return {
        myRequest: myAjaxRequest
    };
}

var mySandbox = sandbox();
mySandbox.myRequest.start();

or it can return a reference through a closure (excellent explanation here), e.g:

function sandbox () {
    var myAjaxRequest = A.io.request(/* constructor... */);

    return {
        getRequest: function () {
            return myAjaxRequest;
        } 
    };
}

var mySandbox = sandbox();
mySandbox.getRequest().start();

As long as you have a reference to your object it will not be garbage collected. You can safely call the start method until the page is unloaded.

Community
  • 1
  • 1
johnhunter
  • 1,826
  • 15
  • 19