如果定义像函数:Ajax(XMLHttpRequest)对象在使用后是否清理?
function fetchData(callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readystate === 4 && xhr.status === 200) {
callback(xhr.responseText);
}
};
xhr.open('GET', 'http://example.com/', true);
xhr.send();
}
现在想象一下,我运行fetchData
功能很多。
所以问题是,我需要/应该在回调执行后运行xhr = null;
吗?
我知道JavaScript有garbage collector但是没有设置xhr
到null
伤害?
这个问题与XMLHttpRequest - freeing after use?类似,但我觉得它没有得到一个答案,说明该对象实际上处于垃圾收集将其删除的状态。
这很可能是一个实现细节,因为我敢肯定规范没有说明会发生什么 –
你会在哪里设置xhr为null?在readystate处理程序中将其设置为null就好比在你站在里面时试图炸毁buildini。 –
一旦'callback'运行,就不再有'xhr'的引用,并且整个范围被收集。 – Brian