Last active
July 4, 2020 12:41
-
-
Save julienetie/69e809db995a66186c890efad48c0026 to your computer and use it in GitHub Desktop.
ho-js.4.js
This file contains hidden or 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
| /* | |
| If you don't want something mutable to remain mutable, create a new context. | |
| */ | |
| const createBasket = (externalMutable) => { | |
| const privateConstant = 123; | |
| let privateMutable = 'abc'; | |
| return { | |
| get getValues(){ // New context | |
| return {privateConstant, privateMutable, externalMutable}; | |
| }, | |
| set setValue (value) { // New context | |
| privateMutable = value; | |
| console.log({privateConstant, privateMutable, externalMutable}); | |
| } | |
| }; | |
| } | |
| let mutatingValue = 128; | |
| const basket = createBasket(mutatingValue); | |
| basket.getValues; // {privateConstant: 123, privateMutable: "abc", externalMutable: 128} | |
| basket.setValue = 987; // {privateConstant: 123, privateMutable: 987, mutable: 128} | |
| basket.getValues; // {privateConstant: 123, privateMutable: 987, mutable: 128} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment