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

288
Vistas
Can a method really delete its parent object from the window object?

While experimenting with keeping functions in separate namespaces and attempting to release the memory consumed by namespaces no longer required, I coded this by accident not even thinking about the fact that it is deleting the namespace N from within one of its own methods, N.loader(). I had the unload('N') in the finally, at first, and after realizing that would be executed only after the new script was injected, I moved the unload() before the injection.

Even if the injected script deletes window.N, it is being executed from within the last .then() of the N.loader() method before the finally is executed.

Thus, I am confused concerning whether or not this is truly deleting the namespace and it's methods from memory, or only making them unreachable from the remaining JS code.

Would you please explain how this is working behind the scenes? How could the parent object of the method be deleted before the method has completed executing?

Thank you.


 "use strict";
 try {
   window.N = new Object();
   window.N.loader = function() {
     fetch(url, {})
     .then( response => {})
     .then( data => {
         return fetch(url,{});
     })
     .then( response => {
     })
     .then( data => {
       unload('N');
       let d = new DOMParser(),
           b = d.parseFromString( data, 'text/html' );
       // inject new script.
       // console.log( window.N ) => undefined.
     })
     .catch( error => {
       console.error('Error:', error);
     })
     .finally( () => {
       // too late to delete window.N.
       // New script already injected.
     });
   }; // Close window.N.loader


   function unload(f) {
     console.log( delete window[f] ); // => true
   }

   window.N.loader();

 } catch (err) {
   console.log(err);
 } finally {
 }

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes, it can - with a caveat. You're not actually deleting the object. Rather, you're just deleting the link between the window property and object in memory.

When you delete, the object at that property will still exist. If nothing else has a reference to it, it'll get garbage collected in a few seconds, usually. If something else still has a reference to it, it won't ever get garbage collected unless something else changes.

For a simpler example:

(() => {
  const theNamespace = {
    fn() {
      delete window.propertyA;
    }
  };
  window.propertyA = theNamespace;
  window.propertyB = theNamespace;
})();
window.propertyA.fn();
console.log(window.propertyB);

or only making them unreachable from the remaining JS code

Right, as long as something has a reference to the same object, the object will remain (for the most part - see mark and sweep). If you delete window.N, other references to window.n will stop working - but if something else has a reference to the same object in some other property or identifier, those references will keep working.

about 4 years ago · Juan Pablo Isaza 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