Skip to content

Instantly share code, notes, and snippets.

@obber
Last active September 29, 2018 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obber/7862ade97a3ec1b8fdb5c7f54a76bc32 to your computer and use it in GitHub Desktop.
Save obber/7862ade97a3ec1b8fdb5c7f54a76bc32 to your computer and use it in GitHub Desktop.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment