Delete instance of a class?

后端 未结 2 796
清酒与你
清酒与你 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.

    0 讨论(0)
  • 2020-12-09 16:36

    Class not same function are different. Not work a delete. Class is system modification.

    class SAFunc {
      method1(){
        console.log("1");
      }
      method2(){
        console.log("2");
      }
    }
    let func  = new SAFunc();
    func['method2']()
    

    Try:

    • delete window['func'] -- not work
    • delete eval['func'] -- not work
    • delete window['SAFunc'] -- not work
    • ...
    • ...

    Function - command work delete

    method1 = function(){
      console.log("func1");
    }
    function method2() {
      console.log("func2");
    }
    var SAFunc = { method3: function() { console.log("func3"); } }
    

    Make ur test... Try:

    • delete window['method1']
    • delete window['method2']
    • delete SAFunc['method3']

    Good fun! i love programming

    Enjoin us ;)

    0 讨论(0)
提交回复
热议问题