Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

880
Vistas
Is there destructor in typeScript

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

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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

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

over 4 years ago · Santiago Trujillo Denunciar

0

As of ES2021, finalizers were added to the specification.

To use that feature, you create a FinalizationRegistry, that notifies you when any of the associated objects get garbage collected.

You can use it like that:

const reg = new FinalizationRegistry((id: number) => {
  console.log(`Test #${id} has been garbage collected`);
});

class Test{
  id: number;
  constructor(id: number){
    this.id = id;
    reg.register(this, this.id);
    //                 ^^^^^^^--- This is the "testament", whatever value, which will be passed to the finalization callback
  }
}

{
  const test1 = new Test(1);
  const test2 = new Test(2);
}

Note that when the callback is called, the object had already been garbage collected; only its "testament" (or as MDN puts it, less dramatically, the "held value") is given to the finalizer.

If you need access some properties of the object in the finalizer, you can store them inside the testament, which, in this case, can be garbage collected just after the original object:

interface TestTestament{
  id: number,
  intervalid: ReturnType<typeof setInterval>
}

const reg = new FinalizationRegistry((testament: TestTestament) => {
  console.log(`Test #${testament.id} has been garbage collected`);
  clearInterval(testament.intervalid);
});

class Test{
  private testament: TestTestament;
  constructor(id: number){
    this.testament = {
      id,
      intervalid: setInterval(() => {
        console.log(`Test interval #${id}`);
      }, 1000)
    };

    reg.register(this, this.testament);
  }
}

{
  const test1 = new Test(1);
  const test2 = new Test(2);
}

Note that the specification doesn't guarantee when garbage collection happens, so the finalizer may not even be called if the object stays in the memory.

over 4 years ago · Santiago Trujillo Denunciar

0

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
        // garbageCollect
        if (global.gc) {global.gc()}
    }


    
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda