Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1.</p> <p>The return value of <code>||</code> is not boolean.</p> <p>It is the first argument if it is truthy or the second argument if it is not.</p> <p>So <code>a || b</code> is equivalent to <code>a ? a : b</code> and <code>a &amp;&amp; b</code> is equivalent to <code>a ? b : a</code>.</p> <p>2.</p> <p>When power is called, a new frame is pushed onto the call stack to hold the paramaters and local variables like other languages. But JavaScript is a bit different from many languages in that when a function call results in a new function instance being created, the new function instance holds a reference to the stack frames on the stack when it is created. Since these stack frames hold locals, there is a different place in memory for functions created by different calls to the same function.</p> <p>For example, in</p> <pre><code>function makeCounter() { var counter = 0; return function () { return counter++; }; } var c1 = makeCounter(); var c2 = makeCounter(); c1(); c1(); c1(); c2(); c2(); alert(c1() + ", " + c2()); // -&gt; 3, 2 alert(c1() + ", " + c2()); // -&gt; 4, 3 </code></pre> <p><code>makeCounter</code> is first called to initialize <code>c1</code>. This creates a stack frame like <code>{ counter: 0 }</code> which the first counter function points to. The second call to <code>makeCounter</code> used to initialize <code>c2</code> creates a different stack frame.</p> <p>So the code above is equivalent to</p> <pre><code>var c1SFrame = { counter: 0 }; var c2SFrame = { counter: 0 }; c1SFrame.counter++; c1SFrame.counter++; c1SFrame.counter++; c2SFrame.counter++; c2SFrame.counter++; c2SFrame.counter++; alert(c1SFrame++ + ", " + c2SFrame++); alert(c1SFrame++ + ", " + c2SFrame++); </code></pre> <p>which should make it obvious why it alerts what it does.</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