Let's take it line by line:
var foo = bar;
All that names of objects do is hold addresses in memory. So in this statement, you are making foo
hold the same memory address as bar
. If for example bar
was holding 0xFF, then via the assignment you would make foo
also hold 0xFF.
However, keep in mind that bar
and foo
are two distinct objects, they happen to just hold the same address in memory.
Now, since they do happen to hold the same address in memory, mutating members of bar
will also affect the members of foo
because they are bound to the same object.
bar.prop = "something entirely different"; // affects foo.prop also
However, here is the key point to remember:
Assignment only changes what the name of the object is bound to, and does not affect any other objects.
In other words, the statement below made bar
hold a different memory address, but this does not affect foo
.
bar = "no longer an object"; // Made bar hold a different memory address
Now, explaining the quote:
Javascript is always pass by value, but when a variable refers to an object (including arrays), the "value" is a reference to the object.
Take this example:
function f(obj1, obj2)
{
obj1.prop = 10;
obj2 = {prop: 20};
}
var bar = {prop: 1};
var foo = {prop: 2};
f(bar, foo);
console.log("bar.prop: " + bar.prop + ", foo.prop: " + foo.prop);
This prints outs: bar.prop: 10, foo.prop: 2
. Inside function f
, obj1 holds the same memory address as bar
and obj2 holds the same memory address as foo
. However, only bar is affected because inside f
only altering members of objects are reflected and assignment obj2 = {prop: 20};
only affects the local variable obj2
not foo
.
In other words, the "value" is a reference to an object
means, altering members affects the referenced object but assigning only affects the local variable.