Luk*_*z B 2 javascript constructor destructor reference-counting javascript-objects
如何在 JavaScript 中实现引用计数?目的是在创建第一个实例或销毁最后一个实例时触发构造函数/析构函数。
我需要实现三种类:抽象类、单例类和普通类(继承或不继承)。
我尝试实现包含所有实例数组的 ClassDispatcher 类,但删除它们并不会删除引用...
Module = function(parentModule)
{
this.destroyInstance = function()
{
Module.prototype.referenceCounter--;
}
this.initInstance = function()
{
Module.prototype.referenceCounter++;
}
this.initInstance();
}
Module.prototype.referenceCounter = 0;
Run Code Online (Sandbox Code Playgroud)
你不能。为了执行您所描述的操作,当对象即将被垃圾收集或释放对其的最后一个引用时,您必须能够从 JavaScript 引擎接收回调。没有这样的回调。
你最好的选择:从更大的角度看你的设计,找到一种不需要这样做的方法。新的 ES2015WeakMap和/或WeakSet对象可能在更大的设计中有用。
非常遥远的第二佳替代方案:如果您可以要求对象的用户调用一个方法,例如destroy,当他们认为已经完成了对对象的引用时,您可以这样做,但这很容易失败,因为使用错误(未能调用destroy)。
这是一个非常容易失败的例子:
var CountedThing = (function() {
var instances = 0;
function CountedThing() {
if (!this.hasOwnProperty("destroyed")) {
++instances;
this.destroyed = false;
if (instances === 1) {
// First instance created
}
}
}
CountedThing.prototype.destroy = function() {
if (!this.destroyed) {
this.destroyed = true;
--instances;
if (instances === 0) {
// Last instance "destroyed"
}
}
}
return CountedThing;
})();
Run Code Online (Sandbox Code Playgroud)
但同样,这很容易失败。它可以工作(看看任何编写良好的 C 程序,它必须以类似的方式管理其内存),但正确地执行它是很棘手的(看看 C 程序中数以百万计的与内存相关的错误)。