parentNode being lost on Javascript inner closure? Chrome bug?
My guess is that testChildEl.parentNode
does not reference testParentEl
strongly, so it's garbage collected.
Both referencing testParentEl
inside the timeout and adding a strong reference to testParentEl
on testChildEl
fix the problem for me:
(function testDoodle() { var testParentEl = document.createElement('div'); var testChildEl = testParentEl.appendChild(document.createElement('div')); setTimeout(function() { testParentEl; // Prevents it from being garbage collected document.write('Has parent? ' + !!testChildEl.parentNode); }, 100);})();
(function testDoodle() { var testParentEl = document.createElement('div'); var testChildEl = testParentEl.appendChild(document.createElement('div')); testChildEl.strongParent = testParentEl; // Prevents garbage collection setTimeout(function() { document.write('Has parent? ' + !!testChildEl.parentNode); }, 100);})();
I believe that this is finally fixed by Chrome v50 (or at least I haven't been able to repro since updating).