Created
September 7, 2019 10:03
-
-
Save kayac-chang/097a5fb6d24cf0a062fa274e0ecaf345 to your computer and use it in GitHub Desktop.
Simple Object property Observer
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
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