Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most of the answers are correct in that passing an object as a function parameter breaks a closure and thus allow us to assign things to functions from within a loop. But I'd like to point out <strong>why</strong> this is the case, and it's not just a special case for closures.</p> <p>You see, the way javascript passes parameters to functions is a bit different form other languages. Firstly, it seems to have two ways of doing it depending on weather it's a primitive value or an object. For primitive values it <em>seems</em> to pass by value and for objects it <em>seems</em> to pass by reference.</p> <h2>How javascript passes function arguments</h2> <p>Actually, the real explanation of what javascript does explains both situations, as well as why it breaks closures, using just a single mechanism.</p> <p>What javascript does is actually it passes parameters <strong>by copy of reference</strong>. That is to say, it creates another reference to the parameter and passes that new reference into the function.</p> <h2>Pass by value?</h2> <p>Assume that all variables in javascript are references. In other languages, when we say a variable is a reference, we expect it to behave like this:</p> <pre><code>var i = 1; function increment (n) { n = n+1 }; increment(i); // we would expect i to be 2 if i is a reference </code></pre> <p>But in javascript, it's not the case:</p> <pre><code>console.log(i); // i is still 1 </code></pre> <p>That's a classic pass by value isn't it?</p> <h2>Pass by reference?</h2> <p>But wait, for objects it's a different story:</p> <pre><code>var o = {a:1,b:2} function foo (x) { x.c = 3; } foo(o); </code></pre> <p>If parameters were passed by value we'd expect the <code>o</code> object to be unchanged but:</p> <pre><code>console.log(o); // outputs {a:1,b:2,c:3} </code></pre> <p>That's classic pass by reference there. So we have two behaviors depending on weather we're passing a primitive type or an object.</p> <h2>Wait, what?</h2> <p>But wait a second, check this out:</p> <pre><code>var o = {a:1,b:2,c:3} function bar (x) { x = {a:2,b:4,c:6} } bar(o); </code></pre> <p>Now see what happens:</p> <pre><code>console.log(o); // outputs {a:1,b:2,c:3} </code></pre> <p>What! That's not passing by reference! The values are unchanged!</p> <p>Which is why I call it <strong>pass by copy of reference</strong>. If we think about it this way, everything makes sense. We don't need to think of primitives as having special behavior when passed into a function because objects behave the same way. If we try to modify the object the variable points to then it works like pass by reference but if we try to modify the reference itself then it works like pass by value.</p> <p>This also explains why closures are broken by passing a variable as a function parameter. Because the function call will create another reference that is not bound by the closure like the original variable.</p> <h2>Epilogue: I lied</h2> <p>One more thing before we end this. I said before that this unifies the behavior of primitive types and objects. Actually no, primitive types are still different:</p> <pre><code>var i = 1; function bat (n) { n.hello = 'world' }; bat(i); console.log(i.hello); // undefined, i is unchanged </code></pre> <p>I give up. There's no making sense of this. It's just the way it is.</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