Will this junk junk code collect what I expect?

function fetchXmlDoc(uri) { var xhr = new XMLHttpRequest(); var async = false; xhr.open("GET", uri, async); xhr.send(); return xhr.responseXML; } 

Basically, when I call this function, does the xhr object get garbage collection, or will it stick forever because the caller is held on xhr.responseXML ? If this is the last, would it allow it?

 function fetchXmlDoc2(uri) { var xhr = new XMLHttpRequest(); var async = false; xhr.open("GET", uri, async); xhr.send(); var xml = xhr.responseXML; return xml; } 

Despite all my years of JS, all memory management still scares me ...

+7
source share
1 answer

The responseXML property of the xhr object is a reference to the actual data object (just as you implicitly accepted in the second code fragment: you are not copying the data, you are copying the link).

Thus, the xhr object will eventually receive garbage collection. There is only one link to it: right here in this function where it was created.

+2
source

All Articles