JavaScript:通过类方法创建和销毁类实例 [英] JavaScript: Create and destroy class instance through class method

查看:110
本文介绍了JavaScript:通过类方法创建和销毁类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何通过类方法删除对象。我希望能够创建一个具有destroy方法的类,该方法可以从内存中释放对象。到目前为止,我所做的研究尚无定论。我知道垃圾收集最终会处理该对象,但是我想采用一种更确定的方式销毁它。

I'm trying to figure out how to delete an object through a class method. I would like to be able to create a class that has a destroy method that releases the object from memory. So far, the research I've done has been inconclusive. I understand that garbage collection will eventually take care of the object, but I'd like a more definitive way to destroy it. Is this possible?

// class constructor
var class = function () {
     this.destroy = function () {};
};

// instance
var instance = new class();
instance.destroy();
console.log(instance); // should be null or undefined


推荐答案

否。 JavaScript是自动垃圾收集的;仅当GC决定运行并且对象有资格进行收集时,该对象的内存才会被回收。

No. JavaScript is automatically garbage collected; the object's memory will be reclaimed only if the GC decides to run and the object is eligible for collection.

看到这将根据需要自动发生,目的是什么显式回收内存的方法?

Seeing as that will happen automatically as required, what would be the purpose of reclaiming the memory explicitly?

这篇关于JavaScript:通过类方法创建和销毁类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆