Javascript 关闭是否导致此处泄漏?

Javascript 关闭是否导致此处泄漏?,javascript,memory-leaks,garbage-collection,Javascript,Memory Leaks,Garbage Collection,我有一个简单的设置来发出AJAX请求,当检查内存使用情况时,快速混合按钮会增加事件侦听器计数和堆使用 显然,Chrome中需要事件侦听器计数,因为GC在分析内存时不使用它,但是堆呢?我是不是漏掉了一个明显的漏洞 function handleResponse(oReq, success_func) { return function() { try { if (oReq.readyState === XMLHttpRequest.DONE) { if (o

我有一个简单的设置来发出AJAX请求,当检查内存使用情况时,快速混合按钮会增加事件侦听器计数和堆使用

显然,Chrome中需要事件侦听器计数,因为GC在分析内存时不使用它,但是堆呢?我是不是漏掉了一个明显的漏洞

function handleResponse(oReq, success_func) {
  return function() {
    try {
      if (oReq.readyState === XMLHttpRequest.DONE) {
        if (oReq.status === 200) {
          var data = oReq.responseText;
          success_func(data);
        }
      }
    } catch (e) {
      console.log('Something went wrong: ' + e.description);
    }
  }
}

function makeRequest(name, method, data, success_func) {
  return function() {
    var oReq = new XMLHttpRequest();
    oReq.onreadystatechange = handleResponse(oReq, success_func);
    oReq.open(method, 'http://localhost:8080/' + name);
    oReq.send(data);
  }
}

function updateBookList(data) {
  console.log(typeof data);
  document.getElementById('ajax-content').innerHTML = data;
}

document.getElementById('show-books').addEventListener(
  'click', makeRequest('get_books', 'GET', null, updateBookList)
);

每次调用返回函数(闭包)的函数时,您都在创建新的结构。它们将一直存在,直到其中的所有变量和对象不再具有任何引用。GC应在一段时间后将其清除

JavaScript的“原型”体系结构允许您在“主”的基础上创建实例。这允许您管理事物的构造和破坏

此外,它还允许您拥有多个唯一的实例,这些实例不会相互冲突

您可以利用ES6类结构。或者,这里是它作为原型的psuedo“类”的外观

this.app = this.app || {};
(function(){

function Req(name, method, data, onSuccess){

    // this is kind of like the constructor
    this.success_func = onSuccess;

    // binding to "this" instance allows the XMLHttpRequest 
    // to trigger the result within the scope of "this" instance. 
    // Otherwise, the "onreadystatechange" method will execute 
    // within the window scope.
    this.handleResponse_bound = this.handleResponse.bind(this);

    this.makeRequest(name, method, data);

}

var p = Req.prototype;

    p.handleResponse = function() {
        try {
          if (this.oReq.readyState === XMLHttpRequest.DONE) {
            if (this.oReq.status === 200) {
              var data = this.oReq.responseText;
              this.success_func(data);
              this.cleanup();
            }
          }
        } catch (e) {
          console.log('Something went wrong: ' + e.description);
        }
    }

    p.makeRequest = function(name, method, data) {
        this.oReq = new XMLHttpRequest();
        this.oReq.onreadystatechange = this.handleResponse_bound;
        this.oReq.open(method, 'https://jsfiddle.net/' + name);
        this.oReq.send(data);
    }

    p.cleanup = function(){
        this.oReq = null;
        this.success_func = null;
        this.handleResponse_bound = null;
    }

    app.Req = Req;

}());

function updateBookList(data) {
  console.log(typeof data);
  document.getElementById('ajax-content').innerHTML = data;
}

document.getElementById('show-books').addEventListener(
  'click', new app.Req('get_books', 'GET', null, updateBookList)
);

我不确定那个代码是否会导致泄漏。我很好奇你为什么需要闭包?通过
makeRequest
handleResponse
返回闭包有什么好处?只是在这里玩一下,但是想法是将它们用于更多的请求(例如在其他事件处理程序上),您可以在不使用闭包的情况下重用这些函数。也许试试看,看看你的堆问题是否消失了。