Skip to content

Instantly share code, notes, and snippets.

@julienetie
Last active July 4, 2020 12:41
Show Gist options
  • Select an option

  • Save julienetie/69e809db995a66186c890efad48c0026 to your computer and use it in GitHub Desktop.

Select an option

Save julienetie/69e809db995a66186c890efad48c0026 to your computer and use it in GitHub Desktop.
ho-js.4.js
/*
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