Created
September 12, 2015 17:01
-
-
Save ebidel/d48d590ff54a6e3370ee 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
function can watch properties, but ignores setAttribute() to change attributes / properties and fails with method calls!
Error message for "setAttribute" call:
Could be handled by get handler like that.