销毁打字稿中的循环引用实例? [英] Destroy an circular-reference instance in typescript?

查看:83
本文介绍了销毁打字稿中的循环引用实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有如下代码:

class A {
    b: B;
    constructor() {
        this.b = new B(this);
    }
}

class B {
    a: A;
    constructor(a: A) {
        this.a = a;
    }
}
let a= new A()

当我想销毁一个实例时:

When I want to destroy instance of a:

1 我应该

a=null; 

a.b.a=null;
a=null;

?

2 有什么方法可以编写代码来测试结果?说一些代码来检测内存中某个类的实例号吗?

2 Is there any way to write code to test results? Say some code to detect instance number of some class in the memory?

推荐答案

如果没有引用a或b,则垃圾收集器会将其标记为不可访问并一起清理,您无需手动执行任何操作.

If there's no reference to a or b it will be marked as unreachable by the garbage collector and be cleaned together, you don't have to do anything manually.

您可以在javascript 此处.

You can find more about how garbage collectors work in javascript here.

这篇关于销毁打字稿中的循环引用实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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