Destroy an circular-reference instance in typescript?

大城市里の小女人 提交于 2019-12-11 04:23:39

问题


Say I have code like following:

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 Should I just

a=null; 

or

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

?

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


回答1:


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.

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



来源:https://stackoverflow.com/questions/40819466/destroy-an-circular-reference-instance-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!