(ES6) ES6 (2016)

GarbageCollection (GC03)

   1:  const element = document.getElementById('myElement');
   2:  const clickHandlers = new WeakMap(); // Use a WeakMap to store event handler
   3:  const handleClick = () => {
   4:      console.log('Element clicked!');
   5:  };
   6:  clickHandlers.set(element, handleClick); // Store the handler in the WeakMap, using the element as a weak key
   7:  element.addEventListener('click', clickHandlers.get(element));



Weak References (WeakMap)
WeakMap will automatically remove handleClick entry after element will be removed from DOM and garbage collected

// ... (the element might be removed from the DOM at some point) ...
// Now, when the element is garbage collected, the WeakMap doesn't prevent it. The entry will be removed automatically

// ... later, when the element is no longer needed, the click handler will be garbage collected automatically





GarbageCollection context:






ES6 context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/JavascriptES6/GC03.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>