Skip to content

Instantly share code, notes, and snippets.

@pwFoo
Forked from ebidel/dom-property-watcher.js
Created April 13, 2021 10:12
Show Gist options
  • Save pwFoo/a12fbd250fe623a0e9f9409f45c05952 to your computer and use it in GitHub Desktop.
Save pwFoo/a12fbd250fe623a0e9f9409f45c05952 to your computer and use it in GitHub Desktop.
DOM property watcher using ES6 proxies.
// 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