Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Object.defineProperty - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
function convert(obj) {
Object.keys(obj).forEach(key => {
let internalValue = obj[key];
Object.defineProperty(obj, key, {
get() {
console.log(`getting key "${key}": ${internalValue}`);
return internalValue;
},
set(newValue) {
internalValue = newValue;
console.log(`setting key "${key}" to: ${internalValue}`);
},
})
});
}
const obj = { foo: 123 };
convert(obj)
obj.foo
obj.foo = 234;
obj.foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.