Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simple answer: if value of the <code>str</code> is not referenced from anywhere else (and <code>str</code> itself is not referenced from <code>restofprogram</code>) it will become unreachable as soon as the <code>function (str) { ... }</code> returns.</p> <p>Details: V8 compiler distinguishes real <em>local</em> variables from so called <em>context</em> variables captured by a closure, shadowed by a <strong>with</strong>-statement or an <code>eval</code> invocation. </p> <p>Local variables live on the stack and disappear as soon as function execution completes.</p> <p>Context variables live in a heap allocated context structure. They disappear when the context structure dies. Important thing to note here is that context variables from the same scope live in the <strong>same</strong> structure. Let me illustrate it with an example code:</p> <pre><code>function outer () { var x; // real local variable var y; // context variable, referenced by inner1 var z; // context variable, referenced by inner2 function inner1 () { // references context use(y); } function inner2 () { // references context use(z); } function inner3 () { /* I am empty but I still capture context implicitly */ } return [inner1, inner2, inner3]; } </code></pre> <p>In this example variable <code>x</code> will disappear as soon as <code>outer</code> returns but variables <code>y</code> and <code>z</code> will disappear only when <strong>both</strong> <code>inner1</code>, <code>inner2</code> <strong>and</strong> <code>inner3</code> die. This happens because <code>y</code> and <code>z</code> are allocated in the same context structure and all three closures implicitly reference this context structure (even <code>inner3</code> which does not use it explicitly).</p> <p>Situation gets even more complicated when you start using <strong>with</strong>-statement, <strong>try/catch</strong>-statement which on V8 contains an implicit <strong>with</strong>-statement inside catch clause or global <code>eval</code>.</p> <pre><code>function complication () { var x; // context variable function inner () { /* I am empty but I still capture context implicitly */ } try { } catch (e) { /* contains implicit with-statement */ } return inner; } </code></pre> <p>In this example <code>x</code> will disappear only when <code>inner</code> dies. Because: </p> <ul> <li><strong>try/catch</strong>-contains implicit <strong>with</strong>-statement in catch clause</li> <li>V8 assumes that any <strong>with</strong>-statement shadows <em>all</em> the locals </li> </ul> <p>This forces <code>x</code> to become a context variable and <code>inner</code> captures the context so <code>x</code> exists until <code>inner</code> dies.</p> <p>In general if you want to be sure that given variable does not retain some object for longer than really needed you can easily <em>destroy</em> this link by assigning <code>null</code> to that variable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload