-
-
Save pwFoo/a12fbd250fe623a0e9f9409f45c05952 to your computer and use it in GitHub Desktop.
DOM property watcher using ES6 proxies.
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
// Watch accesses/sets on a DOM element property. | |
function watchPropsOn(el) { | |
return new Proxy(el, { | |
get(target, propKey, receiver) { | |
//return Reflect.get(target, propKey, receiver); | |
console.log('get', propKey); | |
return el[propKey]; | |
}, | |
set(target, propKey, value, receiver) { | |
console.log('set', propKey, value); | |
target[propKey] = value; | |
} | |
}); | |
} | |
let el = document.createElement('div'); | |
let elProxy = watchPropsOn(el); | |
elProxy.textContent = 'hi there'; | |
console.log(elProxy.textContent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment