Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code is equivalent to the following:</p> <pre><code>var cc; cc = cc || {}; cc = cc; </code></pre> <p>which obviously is a mistake.</p> <p><strong>UPDATE.</strong> I did more research on this topic and here's an interesting thing:</p> <p>Whenever you use <code>var</code> keyword it creates a new variable in current scope unless it already exists <strong>in current scope</strong>. Basically this code:</p> <pre><code>var cc = 1; function test() { var cc = cc || {}; return cc; } test(); </code></pre> <p>will always produce <code>{}</code>, no matter what the initial value of <code>cc</code> ( in global scope ) is. In particular this code:</p> <pre><code>var cc = [expression]; </code></pre> <p>is equivalent to:</p> <pre><code>var cc; cc = [expression]; </code></pre> <p>although <code>var cc;</code> creates new variable <strong>only if</strong> <code>cc</code> does not exist in current scope.</p> <p><strong>UPDATE 2.</strong> What operation takes precedence is confusing because in OP's code both <code>=</code> signs actually are not the same. The first one denotes variable declaration, because there is a <code>var</code> keyword before it. The second one is an assignment. That's why</p> <pre><code>var x = y = z; </code></pre> <p>is equivalent to</p> <pre><code>var x; y = z; x = z; </code></pre> <p>( note that <code>var</code> keyword applies only to <code>x</code> ) while</p> <pre><code>x = y = z; </code></pre> <p>is equivalent to</p> <pre><code>y = z; x = z; </code></pre> <p>( note that operation <code>y=z</code> returns <code>z</code>, which doesn't really matter (it could be <code>y</code> obviously) but it is worth noting )</p> <p><strong>CONCLUSION:</strong> Variable declaration on the left side <strong>always</strong> comes before evaluation of the right side and assignment of the right side to the left side.</p>
 

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