Delete instance of a class?

后端 未结 2 809
清酒与你
清酒与你 2020-12-09 16:03

I have a class that was created like this:

function T() {
    this.run = function() {
        if (typeof this.i === \'undefined\')
            this.i = 0;
           


        
2条回答
  •  庸人自扰
    2020-12-09 16:36

    To delete an instance, in JavaScript, you remove all references pointing to it, so that the garbage collector can reclaim it.

    This means you must know the variables holding those references.

    If you just assigned it to the variable x, you may do

    x = null;
    

    or

    x = undefined;
    

    or

    delete window.x;
    

    but the last one, as precised by Ian, can only work if you defined x as an explicit property of window.

提交回复
热议问题