Skip to content

Instantly share code, notes, and snippets.

@jbmilgrom
Last active September 23, 2019 16:48
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 jbmilgrom/576cb8ec7866f20f91f780c8426f145d to your computer and use it in GitHub Desktop.
Save jbmilgrom/576cb8ec7866f20f91f780c8426f145d to your computer and use it in GitHub Desktop.
Example of "sameness" in the context of mutability
/**
* With two _objects_, check equality by checking whether they are in fact the same object i.e. the reference.
*/
const areAccountsEqual = (a: BankAccount, b: BankAccount) => a === b;
const elainesAccount = new BankAccount(100);
const georgesAccount = new BankAccount(100);
elainesAccount.checkBalance(); // 100
georgesAccount.checkBalance(); // 100
areAccountsEqual(elainesAccount, elainesAccount); // true
areAccountsEqual(elainesAccount, georgesAccount); // false (eventhough identical funds)
georgesAccount.withdraw(75);
elainesAccount.checkBalance(); // 100
georgesAccount.checkBalance(); // 25
areAccountsEqual(elainesAccount, elainesAccount); // true
areAccountsEqual(elainesAccount, georgesAccount); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment