Skip to content

Instantly share code, notes, and snippets.

@kayac-chang
Created September 7, 2019 10:03
Show Gist options
  • Save kayac-chang/097a5fb6d24cf0a062fa274e0ecaf345 to your computer and use it in GitHub Desktop.
Save kayac-chang/097a5fb6d24cf0a062fa274e0ecaf345 to your computer and use it in GitHub Desktop.
Simple Object property Observer
const {defineProperty} = Object;
function observe({key, value, onChange}, it) {
const descriptor = {
get() {
return value;
},
set(newValue) {
value = newValue;
onChange.call(it, value);
},
};
descriptor.set(value);
return defineProperty(it, key, descriptor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment