html - Is inline javascript garbage collected when document body is replaced? -
consider have below webpage written in html (body section only):
<body> <p> ... </p> <script> function fn() { // stuff } </script> </body>
now, if replace innerhtml
of document.body
javascript say, div, body part becomes:
<body> <div> ... </div> </body>
... fn
object eligible garbage collection if no references exist anywhere in rest of code (any context)?
it subject garbage collection if no other references made context. however, there 1 small reference holding on function, global window object. because function (and entire script section shown) scoped globally. reference still exist even if entire document.body's innerhtml replaced.
there few ways free object shown in exact example global object , make eligible collection. deleting property on global object not option because declared function , not property.
- overwrite property on global object else.
window.fn = [][0];
(undefined shortcut) - navigate away page global object becomes eligible garbage collection.
quick note on garbage collection, occurs when browser feels necessary, , not after object has no references (or @ least, not - may happen time time).
Comments
Post a Comment