Take a look:
var a = 'hello';
var b = a;
a = 'world';
console.log(b); // what does this log?
Object property assignments behave like variables. It's conceptually sound to think of assigning a property of an object to a value to behave much like assigning a variable to a value.
var tim = {
name: 'Tim',
};
var someNumber = 16;
tim.age = someNumber;
someNumber = 17;
console.log(tim.age); // what does this log?
Assigning a variable to an object means keeping a reference (pointer) to the key-value store (that exact instance of an Object)
Take a look at these two exmaples:
// EXAMPLE 1
var objA = {
message: 'cats'
};
var objB = objA;
objA.message = 'dogs';
console.log(objB.message); // what does this log
// EXAMPLE 2
var objA = {
message: 'cats'
};
var objB = objA;
objB.message = 'dogs';
console.log(objA.message); // what does this log
Function arguments are variable assignments that are scoped to the enclosing function, to whichever value was passed when invoked.
var run = function(arg) {
arg = [4,5,6];
};
var nums = [1,2,3];
run(nums);
console.log(nums); // what does this log
Just like variables that point to objects, a function argument is also assigned to the object reference passed in.
var run = function(arg) {
arg[0] = 0;
};
var nums = [1,2,3];
run(nums);
console.log(nums); // what does this log