Home > Enterprise >  Can a method really delete its parent object from the window object?
Can a method really delete its parent object from the window object?

Time:05-19

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 {
 }

CodePudding user response:

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.

  • Related