0

xmlHttp はガベージ コレクションを取得しますか?

function foo (param)
{
    var xmlHttp = GetXmlHttpRequestObject();

    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
        {
            // do something involving param and xmlHttp.responseXML
        }
    }

    xmlHttp.open("GET", "GetAsyncData.ashx", true);
    xmlHttp.send();
}
4

1 に答える 1

1

Yes, the garbage collector will destroy automatically a variable as soon as the function foo has been executed (since the variable is local and thus it cannot be used outside the given scope).

This is also one of the reasons why is a good practice to especially use local variables declared with var keyword.

Please note that in your specific context, since you perform an asynchronous ajax call, your function is returning while the ajax call is still running so the garbage collector will destroy your xmlHttp variable when the both the function is returned and the call has been completed

于 2013-02-27T08:34:40.337 に答える