Javascript typeScript 中有析构函数吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28885625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:32:27  来源:igfitidea点击:

Is there destructor in typeScript

javascripttypescriptdestructor

提问by Gábor Lupák

Is there destructor in TypeScript? If not, how can I delete an object? I tried destructor()and ~ClassName()but it didn't work.

TypeScript 中有析构函数吗?如果没有,如何删除对象?我试过了destructor()~ClassName()但没有用。

回答by Ryan Cavanaugh

JavaScript uses garbage collection to automatically delete objects when they are no longer referenced. There is no concept of destructors or finalizers.

JavaScript 使用垃圾回收来自动删除不再被引用的对象。没有析构函数或终结器的概念。

You can't observe when an object is deleted by the garbage collector, nor is it predictable.

您无法观察垃圾收集器何时删除对象,也无法预测。

回答by Thomas Thornier

You can actually

你实际上可以

    class MyClass {
        constructor(input1, input2){
             this.in1 = input1;
             this.in2 = input2;
         }

    }
    let myObject = {};


    try {
         myObject = {
             classHandler: new MyClass('1','2')
         }
    } catch (e) {
    } finally {
        delete myObject.classHandler
    }