Why are objects’ values captured inside function calls?

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 why this is the case, and it’s not just a special case for closures.

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 seems to pass by value and for objects it seems to pass by reference.

How javascript passes function arguments

Actually, the real explanation of what javascript does explains both situations, as well as why it breaks closures, using just a single mechanism.

What javascript does is actually it passes parameters by copy of reference. That is to say, it creates another reference to the parameter and passes that new reference into the function.

Pass by value?

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:

var i = 1;
function increment (n) { n = n+1 };
increment(i); // we would expect i to be 2 if i is a reference

But in javascript, it’s not the case:

console.log(i); // i is still 1

That’s a classic pass by value isn’t it?

Pass by reference?

But wait, for objects it’s a different story:

var o = {a:1,b:2}
function foo (x) {
    x.c = 3;
}
foo(o);

If parameters were passed by value we’d expect the o object to be unchanged but:

console.log(o); // outputs {a:1,b:2,c:3}

That’s classic pass by reference there. So we have two behaviors depending on weather we’re passing a primitive type or an object.

Wait, what?

But wait a second, check this out:

var o = {a:1,b:2,c:3}
function bar (x) {
    x = {a:2,b:4,c:6}
}
bar(o);

Now see what happens:

console.log(o); // outputs {a:1,b:2,c:3}

What! That’s not passing by reference! The values are unchanged!

Which is why I call it pass by copy of reference. 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.

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.

Epilogue: I lied

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:

var i = 1;
function bat (n) { n.hello = 'world' };
bat(i);
console.log(i.hello); // undefined, i is unchanged

I give up. There’s no making sense of this. It’s just the way it is.

Leave a Comment