闭包变量内存影响

Closures variables memory impact

我正在开发一个简单的 api 来创建和管理一些 html 的创建(真的没关系..)。

无论如何,这个 api 包含在一个 UMD 片段中,它基本上接收一个匿名函数作为其参数之一:

看一下真题代码:

'queues' 在内存方面会发生什么情况:

  1. 初始状态转为"after usage"状态?
  2. 如果 "someUniqueKey1" 对象被传递给 jQuery 的某个匿名函数引用?

代码:

+function( root, factory ) {
  if ( typeof define === 'function' && define.amd ) {
    // AMD. Register as an anonymous module.
    define( [ 'jquery' ], factory );
  } else if ( typeof exports === 'object' ) {
    // Node. Does not work with strict CommonJS, but
    // only CommonJS-like environments that support module.exports,
    // like Node.
    module.exports = factory( require( 'jquery' ) );
  } else {
    // Browser globals (root is window)
    root.Alert = factory( jQuery );
  }
}( this, function( $ ) {
    var queues = {}; // <-- initial state

    // after usage state:
    queues = {
      "someUniqueKey1": { "someProperty": [] },
      "someUniqueKey2": { "someProperty": [] },
    }

    // constructor some here...

    // prototyping goes here..

    // ....

    return alertAPI
  }
);

我尝试使用 chrome 开发工具进行分析,但我只能看到创建的实例以及删除它们时发生的情况。

如果没有引用与任何 variable/object 关联,则浏览器会将其标记为垃圾回收。您可以在此处了解更多信息 What is JavaScript garbage collection?