Skip to content

Instantly share code, notes, and snippets.

@battmanz
Last active October 17, 2018 21:28
Show Gist options
  • Save battmanz/c5046c2d0af45938190e1178ab9cb007 to your computer and use it in GitHub Desktop.
Save battmanz/c5046c2d0af45938190e1178ab9cb007 to your computer and use it in GitHub Desktop.
Demonstrates the difference between an immutable object and a variable that cannot be reassigned
'use strict';
// CASE 1: The object is mutable and the variable can be reassigned.
let o1 = { foo: 'bar' };
// Mutate the object
o1.foo = 'something different';
// Reassign the variable
o1 = { message: "I'm a completely new object" };
// CASE 2: The object is still mutable but the variable cannot be reassigned.
const o2 = { foo: 'baz' };
// Can still mutate the object
o2.foo = 'Something different, yet again';
// Cannot reassign the variable
// o2 = { message: 'I will cause an error if you uncomment me' }; // Error!
// CASE 3: The object is immutable but the variable can be reassigned.
let o3 = Object.freeze({ foo: "Can't mutate me" });
// Cannot mutate the object
// o3.foo = 'Come on, uncomment me. I dare ya!'; // Error!
// Can still reassign the variable
o3 = { message: "I'm some other object, and I'm even mutable -- so take that!" };
// CASE 4: The object is immutable and the variable cannot be reassigned. This is what we want!!!!!!!!
const o4 = Object.freeze({ foo: 'never going to change me' });
// Cannot mutate the object
// o4.foo = 'talk to the hand' // Error!
// Cannot reassign the variable
// o4 = { message: "ain't gonna happen, sorry" }; // Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment