Last active
September 23, 2019 16:48
-
-
Save jbmilgrom/576cb8ec7866f20f91f780c8426f145d to your computer and use it in GitHub Desktop.
Example of "sameness" in the context of mutability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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